Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Infinite yield with WaitForChild issue... Any clue?

I have an issue with the WaitForChild function. When I use It to get a character's object, I freezes and returns this:

Infinite yield possible on 'SnyFort:WaitForChild("LowerTorso")

So these are the parts of the code that cause this:

Server script

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		remoteEvent:FireClient(player,"setUpPlayer")
	end)
end)

Local Script (Starter player script)


local status = "dead"

local torso = nil
local root = nil		
local rightShoulder = nil
local leftShoulder = nil
local neck = nil
local upperTorso = nil
local humanoid = nil

function setUpPlayer()
	local char = player.Character or player.CharacterAdded:wait()
	
	torso = char:WaitForChild("LowerTorso")
	root = char:WaitForChild("HumanoidRootPart")
	rightShoulder = char:WaitForChild("RightUpperArm").RightShoulder
	leftShoulder = char:WaitForChild("LeftUpperArm").LeftShoulder
	neck = char:WaitForChild("Head").Neck
	upperTorso = char:WaitForChild("UpperTorso").Waist
	humanoid =  char:WaitForChild("Humanoid")

	status = "none"

	humanoid.Died:Connect(function()
		status = "dead"
		humanoid.Parent:Destroy()
	end)
end

local functionList = {
	setUpPlayer = setUpPlayer
}

local function onNotifyPlayer(func,...)
	functionList[func](unpack({...}))
end

remoteEvent.onClientEvent:Connect(onNotifyPlayer)

This is how It works: everytime the player respawns, the servers sends them an event to execute the function "setUpPlayer". This function simply fills up each variable.

I've tested It, and It works the first time the character spawns.However,if you reset and respawn, the scripts gets stuck at the first WaitForChild call. And at first sight, It makes no sense, since the CharacterAdded function confirms the character is fully loaded.

Any ideas? Thanks

Edit: I forgot to add the "humanoid.Parent:Destroy()" line.

2 answers

Is your player R15 or R6? Your character should be R15 for this to work. Also, on line 38 you said remoteEvent.onClientEvent:Connect(onNotifyPlayer). I'm pretty sure it should be OnClientEvent, not onClientEvent.

2 comments

0

Yes, It works with R15. And both OnClientEvent and onClientEvent work

0

Try looking at masterjosue1998's answer.

Just changed the line local char = player.Character or player.CharacterAdded:wait() to local char = player.CharacterAdded:wait() and now It works fine... Have no idea why though

This discussion is preserved as it was. New answers, comments, and votes are closed.