App.root not defined?

I am attempting to do multiplayer for my game. But for whatever reason “this.app.root” is not working
Code:
https://playcanvas.com/editor/code/776136?tabs=47225725&line=38&col=31&error=true

Hi @SkooterBahgs,

You need to define the context on your event listener, you can do it using .bind(this):

    socket.on('posMove', function(data){
        var player = this.app.root.findByName('Root').findByName(data.name);
        player.rigidbody.teleport(data.all);
    }.bind(this));

Hey @SkooterBahgs, in addition to @Leonidas’s answer, if you need to use the this keyword for something else, you can also define another variable containing the this object from PlayCanvas and use that instead, like so -

  var self = this;
  socket.on('posMove', function(data){
        var player = self.app.root.findByName('Root').findByName(data.name);
        player.rigidbody.teleport(data.all);
  });