Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

WaitForChild With Player.Character?

Asked by 8 years ago

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

Log in to vote
4
Answered by 8 years ago

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")
1
light.Parent = char:WaitForChild("Torso")* UserOnly20Characters 890 — 8y
0
YellowTide loves WFC unmiss 337 — 8y
0
gg YellowTide LOL XToonLinkX123 580 — 8y
0
It's just a fetish ok.. YellowoTide 1992 — 8y
Ad
Log in to vote
1
Answered by 8 years ago

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.

Answer this question