Open MerelyRBLX opened 10 years ago
Just ran into this bug again on Scripting Helpers. A user was trying to weld parts to the arms when he spawned from a localscript (since they were visible only to the client), but it turned out that the old Character was still intact, so the arms being cloned were the old arms that were about to be destroyed. https://scriptinghelpers.org/questions/10693/help-with-local-scriptshelp
Whenever a character dies and respawns, the LocalScripts in StarterGui and StarterPack are cloned to the player's PlayerGui and Backpack, and often run before the old character has been removed.
This leads to race conditions where game.Players.LocalPlayer.Character exists when the script first runs, but is removed within a few seconds and replaced with a new character.
Repro steps:
Start Server in Studio. Place this in a LocalScript in StarterGui. Add a new player.
The first time it runs, "Workspace" will be printed both times.
Now, kill the player with the command bar. This time it prints "Workspace" and "nil." This is because Player.Character references the old character that died. To get around this, scripters are forced to write hacks like
This is very unpredictable behavior that scripters do not anticipate. If the old player has been removed, we assume that the Player.Character property would point to nil.