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

Roblox is telling me Players is not a valid member of Workspace. Why?

Asked by 8 years ago
Edited 7 years ago

I put in this code:

Workspace.Player.Humanoid.Health = 0

and when I run it Roblox comes up with this error:

22:28:21.376 - Player is not a valid member of Workspace

WHY!?

Note: Yet when I put in a wait it seems to work perfectly fine. WHY!?

0
Well obviously the code is running before the player is in, so that's why it works with the wait. Just a heads up, this won't work in online mode. NinjoOnline 1146 — 8y
0
Thanks for the heads up HaloStorm22 10 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago

The player doesn't get added to the workspace immediately.

To wait for a player to be loaded into the workspace, you have a few options.

1) WaitForChild

workspace:WaitForChild("Player") -- not recommended

2) CharacterAdded event

player.CharacterAdded:connect(function(character)
    print("Character loaded!")
end)

3) Waiting for player.Character to not be nil

while not player.Character do
    wait()
end
Ad

Answer this question