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.
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?
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…
Here’s what my character looks like in the explorer:
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.
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?
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?
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.