WaitForChild() Issue

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? How to Use WaitForChild(), Corectly. and how to use waitforchild for waiting boolvalue with player.Name
game.Players.PlayerAdded:Connect(function(player) ---- script1 
 local a = Instance.new(BoolValue)
 a.Parent = workspace
 a.Name = player.Name
end
game.Players.PlayerAdded:Connect(function(player) --------- script2
 local a = workspace:WaitForChild() --- player name ??
end

now there will be a problem and that is the players character is named the same thing so you could do a.Name = (player.Name…“s bool value”)
and then workspace:WaitForChild(player.Name…“s bool value”)

1 Like

You can also use Player.CharacterAdded to get the character inside the PlayerAdded event.

This right here, your original method isn’t reliable as it limits what you can do, or adds unnecessary work.

If you want to get the character, then you do workspace:WaitForChild(player.Name) in script2, as this will wait for a child in workspace with the same name as the player. You put a string value in WaitForChild to do this (in this case being player.Name).

Some criticism I would give is that you should always do game:GetService("Players") instead of game.Players. Do this for every service except workspace, as that is already a variable. As well as this, when creating an instance, you should set the parent of it AFTER you set the values to it. The reason why can be seen in this post: PSA: Don't use Instance.new() with parent argument

Hope this helps!

1 Like

:WaitForChild() can be used to wait for a child that’s being cloned, such as the player’s PlayerGui, and it’ll be automatically defined once added. If the child wasn’t added for a long time, a warning outputs as it yields for it (waits).

If you want to define the player’s character, I suggest using Player.CharacterAdded event - that way it’ll fire every time they respawn/reset, and in case you have a statue of yourself in workspace.