Anaminus / roblox-bug-tracker

Formerly an unofficial bug tracker for Roblox.
31 stars 24 forks source link

LocalScripts in StarterGui/StarterPack run before old character is removed #210

Open MerelyRBLX opened 10 years ago

MerelyRBLX commented 10 years ago

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.

print("LocalScript running")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
print(Character.Parent)
wait(5)
print(Character.Parent)

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

local Player = game.Players.LocalPlayer
local Character = Player.Character
if not Character or Character.Parent == nil then
    Character = Player.CharacterAdded:wait()
end

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.

MerelyRBLX commented 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