Issue accessing player position using world.getLocalPlayer()
Hello! I'm new to scripting using Typescript in the desktop editor and I've run into a problem that I haven't quite been able to solve. I saw code snippets and suggestions about using world.getLocalPlayer() but it's not quite working for me. When I call that method, the resulting player has an empty name and trying to access its position with player.position.get() gives an error.
Here's the code that isn't working, inside Start :
const player = this.world.getLocalPlayer();
console.log(`local player name: ${player.name.get()}`);
console.log(`local player id: ${player.id}`);
console.log(`local player exists?: ${player}`);
if (player) {
//const position = player.position.get(); //this line gives an error when uncommented
//console.log(`Player position: ${position.x}, ${position.y}, ${position.z}`);
}
Here's the console output for that, when the error is commented out:
Here's the error if I attempt to access the position:
It may be something silly, but I have no idea what's wrong!
I also tried using a player map (code below) that I found and that does work and give me my name and position, however I don't know how I could use that without knowing who is the local player.
this.connectCodeBlockEvent(
this.entity,
hz.CodeBlockEvents.OnPlayerEnterWorld,
(player: hz.Player) => {
this.playerMap.set(player.id, player);
console.log(`added player: ${player.name.get()}`);
const position = player.position.get();
console.log(`Player id: ${player.id}`);
console.log(`Player position: ${position.x}, ${position.y}, ${position.z}`); //this DOES work unlike the other code
},
);
If anyone's able to help I'd be grateful! Thanks for reading