Infinite yield for unknown reason

I’m trying to grab a players character when they enter the game, but I’m getting an infinite yield possible in my output when using the following script.

game.Players.PlayerAdded:Connect(function(plr)
print(plr:WaitForChild("Character").Name)
local part = Instance.new("Part")
part.Position = workspace:WaitForChild(plr).HumanoidRootPart + (Vector3.new(2,1.5,2.5))
end)
1 Like

Replace workspace:WaitForChild(plr) with plr.Character.

And remove the print as well, since you can’t wait for a property of an instance; characters aren’t children of players.

1 Like

WaitForChild will yield until it finds the specified child, the reason it’s breaking is because :WaitForChild() looks for an object inside the parent, you can’t access properties with :WaitForChild() and you’re attempting to access the .Character property with :WaitForChild().

2 Likes

I have the print in there for testing, does it affect the script?

The print is fine.

30characters

2 Likes

I never knew the Character of a Player was a property. But whenever I try to use just plr.Character I get nil in return.

It does affect the script, because there is no child of the player whose name is Character, or at least one doesn’t exist naturally.

1 Like

No, he is attempting to print the player’s name. So just changing plr:WaitForChild("Character") to plr.Character is perfectly okay.

2 Likes

If I use plr.Character than I get the following error:
15:38:07.461 - ServerScriptService.Stand:18: attempt to index field ‘Character’ (a nil value)

Can you provide an example of your code again?

2 Likes

You have to wait for the character to be added using the CharacterAdded event of the player, like so:

local character = plr.Character or plr.CharacterAdded:Wait()

You can also connect a function to it too, like you would part.Touched:

plr.CharacterAdded:Connect(function(character)
    -- do stuff here
end)
1 Like
game.Players.PlayerAdded:Connect(function(plr)
	print(plr.Character.Name)
	local part = Instance.new("Part")
	part.Position = workspace:WaitForChild(plr).HumanoidRootPart + (Vector3.new(2,1.5,2.5))
end)

You should do what @goldenstein64 said.

Here is a working example of your code using his method:

 game.Players.PlayerAdded:Connect(function(plr)
    local character = plr.Character or plr.CharacterAdded:Wait()
	print(character.Name)
	local part = Instance.new("Part")
	part.Position = character.HumanoidRootPart.Position + (Vector3.new(2,1.5,2.5))
end)
3 Likes

So I could do

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
--code
end)
end)
1 Like

You would also have to use the position of the humanoid root part:

part.Position = character.HumanoidRootPart.Position + Vector3.new(2,1.5,2.5)

as well as parent the part after it is created:

part.Parent = workspace
2 Likes

If you wish to do so, yes.

Edit: Other than that I believe @goldenstein64 answered your primary questions.

2 Likes

Alright, I think I’m going to go with that. Thank you @CleverSource and @goldenstein64!

2 Likes

No problem, if you plan to mark someone as a solution I recommend marking goldenstein as he did initially answer the main question. :+1:

2 Likes

Player characters just copy the name of the player. So just print(plr.Name) should suffice. Even print(plr), since tostring(Player) returns Player.Name