Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
Test Code
devSeldom's answer is correct but there's a better way.
With dev's answer it's does 'wait()' regardless of if the character is already there, delaying your script
local char = player.Char or game.Workspace:WaitForChild(player.Name)
This sets character to player.Character if it exists, otherwise it waits for the character to spawn in game.Workspace
Your problem is, you're using FindFirstChild not WaitForChild
light.Parent = charWaitForChild("Torso")
There's actually a better way to handle this than using WaitForChild
with the character's name! An object could potentially be added to workspace with the same name as one of the players, which would cause unpredictable behavior for certain users. ROBLOX has an event in the player called CharacterAdded
, which fires every time that player's character is loaded into Workspace. In addition, all events have a wait
method, which halts script execution until that event is fired.
Putting all this together, if you want to wait until the character is loaded, set up your script as follows:
local plr = game.Players.LocalPlayer plr.CharacterAdded:wait() wait() local char = plr.Character local mouse = plr:GetMouse()
The wait()
was added because I've found that the event can fire slightly early (though it's inconsistent), and thus waiting for the next heartbeat will avoid any issues related to that.