Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

WaitForChild With Player.Character?

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

2 answers

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")

4 comments

1

light.Parent = char:WaitForChild("Torso")*

0

YellowTide loves WFC

0

gg YellowTide LOL

0

It's just a fetish ok..

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.