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

Why does my code not work after I get "Infinite yield possible"?

Asked by
lilzy7 68
4 years ago

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!!!

2 answers

Log in to vote
3
Answered by
Elyzzia 1294 Moderation Voter
4 years ago
Edited by youtubemasterWOW 4 years ago

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.

0
Thank you! lilzy7 68 — 4y
Ad
Log in to vote
0
Answered by
o_fex 146
4 years ago

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

Answer this question