#Why isn't !initialSpawn working?
1 messages · Page 1 of 1 (latest)
initialSpawn is any player join, not just the first- playerSpawn event catches respawns as well as joins, so that helps separate the two
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.
Is there an online example of that? I'm new to scripting so to be honest I have no idea what you just said in that second message. Besides that, your explanation behind initialSpawn makes sense, thank you!
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
So the const InitializedPropertyID = "giga:initialized" part is the creation of the dynamic property and the giga:initialized is that new property?
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