Question: Does an infinite yield happen when the game can't find the instance's child?

Let me explain further… So I have a script that makes the players character explode when a part is touched. After 5 seconds of the function triggering, a message appears that “infinite yield is possible on…”. Now here comes my question, does an infinite yield happen when the game can’t find the instance’s child or the child does not exist?
I am not sure why it is saying that “infinite yield is possible” because “Humanoid” does exist. Anyway, I would be contented if anybody could help me please and answer my questions. Thanks!

Script:

local players = game:GetService("Players")
local block = game.Workspace.Food

block.Touched:Connect(function(hit)
	if hit.Parent:WaitForChild("Humanoid") ~= nil then --Line with the yield function
		local plr = players:GetPlayerFromCharacter(hit.Parent)
		if plr ~= nil and plr.Character:WaitForChild("Humanoid").Health ~= 0 then
			local explosion = Instance.new("Explosion")
			explosion.Position = plr.Character.HumanoidRootPart.Position
			explosion.Parent = workspace
		end
	end
end)

Yes. If the humanoid isn’t found within 5 seconds, a warning will be printed, but the thread will continue waiting for the humanoid to be created.
The block could be getting touched by an accessory’s handle, in which case hit.Parent would equal the accessory. For this reason, I normally use hit:FindFirstAncestorWhichIsA("Model"):FindFirstChild("Humanoid") instead.
FYI, :WaitForChild also takes a second parameter: a maximum duration to wait for the child. If you set it to any number (e.g. 5) and the child still doesn’t exist within that number of seconds, it will return nil.

It happens when you are waiting (yielding) indefinitely. The error you get comes from when you wait for an instance and the instance doesn’t exist within a number of seconds.

1 Like

So if 5 seconds pass then it STOPS looking for the child completely and if you touch the part again it won’t even start looking for it as it is already nil?

It will stop waiting and return nil after 5 seconds, but once it is touched again, it will wait for the child for another 5 seconds.

So what your trying to say is that it’s not the accessory that has the Humanoid but the actual players model? but then why does my function work, wouldn’t it not work?

I am pretty sure we were talking about the second parameter included, I just did not add it… Also, we were talking on what does the second parameter do… So ye…

1 Like

I missed that part my bad

char limit char limit

2 Likes

Here’s what my character looks like in the explorer:
image

As you can see, most of the meshes are a child of the character model. If any of them touch the block, then hit.Parent:WaitForChild("Humanoid") will return the humanoid, since the humanoid is also a child of the model. So when these meshes touch the block, your code works.

However, there are also accessories that have meshes parented to them. If those meshes touch the block, then hit.Parent:WaitForChild("Humanoid") will yield forever, since hit.Parent equals the accessory and the accessory doesn’t have a humanoid.

So if a mesh that is parented to an accessory touches the block then the code won’t work right?

Correct. You use FindFirstAncestorWhichIsA("Model") to work around this.

1 Like

Ok, last question. I know that “infinite yield possible on…” means that a thread is searching for a child but can’t find it. But then why does it print that message in the output if the function worked? If the function worked, that means that hit.Parent:WaitForChild(“Humanoid”) is not equal to nil. Shouldn’t the message show up when the function does not work, because after not finding the child it returns nil?

From the docs:

  • If a call to this method exceeds 5 seconds without returning, and no timeOut parameter has been specified, a warning will be printed to the output that the thread may yield indefinitely.

It will print a warning when there might be an infinite yield. When you provide a timeOut, it is not infinite, since it will stop waiting after x seconds.

Oh, so even if the function works, it’s just warning that the thread MAY yield indefinitely and not WILL yield indefinitely? So, basically it is just a warning that the thread MIGHT yield indefinitely because a wrong chracter’s child could possibly hit the part?

Yes. It will yield indefinitely if a child is never found.

Can you clarify what you mean by this?

I meant that the message warns that there COULD be an infinite yield and not that there is one right now. Doesn’t matter if it finds the child or doesn’t.

Yes, that is correct.
Let me know if you have any other questions!

1 Like

Thanks for helping me out! This is quite a hard topic for me, so ye…

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.