Ok, so what I did was when a player joins then I put in my variables and then make it so a robot gets cloned and spawns near them, but when I run it, I get "03:25:55.643 - Infinite yield possible on 'Players.lilzy7:WaitForChild("Character")' " and then my code deactivates (does not clone the robot when I hit Play test button and wait) and doesn't print anything
local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(player) local plr = player:WaitForChild("Character") local humroot = plr:WaitForChild("HumanoidRootPart") wait(2) print("yay") local robot = game.ServerStorage["Robot Follower"]:Clone() robot.HumanoidRootPart.Position = humroot.Position + Vector3.new(math.random(-50,50),0,math.random(-50,50)) robot.Parent = game.Workspace print("yay") end)
(the robot is a dummy)
Please help and thank you!!!
The problem is that the character isn't a child of the player, so WaitForChild is stuck waiting for a child named "Character" forever.
Rather, it's a property of the player.
if you want to get the character, you just use player.Character
.
Or, if you need to wait for it to spawn, use player.CharacterAdded:Wait()
, since the CharacterAdded event also returns the new character.
Hey there.
WaitForChild will yield until it finds the specified child, the main reason is because WaitForChild looks for an object inside the parent of the player and you can't access properties with WaitForChild.
Try using player.Character