#Why isn't !initialSpawn working?

1 messages · Page 1 of 1 (latest)

icy pasture
#

I'm trying to get players to have a certain score when they first join a world, and never again even if they rejoin. My script however keeps resetting their score every single time they join the world. Isn't intialSpawn the very first time they join the world? Why isn't this working?

meager vapor
#

initialSpawn is any player join, not just the first- playerSpawn event catches respawns as well as joins, so that helps separate the two

torn thistle
#

That wasn't their question; initialSpawn is true when the player first connects. Respawning after death has this set to false

#

What you could do is gate the initialization sequence behind a dynamic property. When you initialize the player's scores, set that dynamic property to true so the condition fails when they rejoin.

icy pasture
torn thistle
#

Dynamic properties are similar to scoreboards; they can be assigned to a player, and they can only hold one value at a time. However, there are a wider range of values you can set.
Dynamic properties are defined by usage; simply set one and it's prepared.

const InitializedPropertyID = "giga:initialized";

world.afterEvents.playerSpawn.subscribe(initializePlayer);
function initializePlayer({initialSpawn, player}) {
  if (initialSpawn && !player.getDynamicProperty(InitializedPropertyID)) {
    player.runCommand("scoreboard players set @s Lives 10");
    player.setDynamicProperty(InitializedPropertyID, true);
  }
}
#

player.getDynamicProperty(...) will return undefined if no property is set; this is truthy when negated. Afterwards it is set to true, so the condition fails on subsequent accesses

icy pasture
torn thistle
#

Sort-of. That variable is just a string, and it holds no special value on its own. I used it to make it clear we're referring to the same property in both places

#

You could forgo the variable and put the same string in both getDynamicProperty and setDynamicProperty ("giga:initialized") and it would behave the same

#

The creation of the property happens when we invoke setDynamicProperty. Then it goes from being undefined to holding some value

#

Otherwise yes, you could say that the property is "giga:initialized", and you can retrieve the property's value by using that identifier inside getDynamicProperty on the player