I have a line of code in a script with a CharacterAdded() function that waits for a Powerup tool in a player’s Backpack when they would happen to die and respawn as shown:
Powerup = player.Backpack:WaitForChild("Powerup")
In my game, the player may not die at all so yielding forever is a thing, but I need a way to access the Powerup that shows up in the Backpack coming from the StarterGear after a player may happen to die.
The issue is that I always get the warning of an Infinite yield possible. My script works exactly how I want it with no problems, but I can’t seem to find a way to disable this warning. It can be quite annoying to see it everytime I playtest my game.
Conversely, is there another way I should be going about this?
You could add a second argument that determines the maximum yield duration before returning nil. I would probably set that to a very high number, such as 1e7.
Yeah, only problem is that in my game, the :WaitForChild() could go on for literally forever because the player might never die, so I can’t really name a time out.
Hold on, you mean that once the game is starting for first time, you already got the infinite yield warning, without the player dying/respawning or something?
Means that you are trying to get that tool’s instance even when it doesnt exist in the player’s gear?
You should not do that. That :WaitForChild parent should be done once the player collects the Tool, not before when the player doesnt have it
EDIT @CaptainSwoosh
What about just using local tool = Gear:FindFirstChild instead of waiting for it? if tool == false then do nothing
Why using WaitForChild, if you are not expecting to wait for the tool cause that doesnt even exist?
Yeah, that’s what I was thinking if I stick with it. Are there any performance issues or other issues with putting the TimeOut as a very very high number?
function waitForChild(parent,childName,timeOut)
local timer = 0
repeat
wait(.00001)
timer += .00001
until parent:FindFirstChild(childName) or timer >= timeOut
end
which could have performance implications if used in mass
Thanks for your help everyone. I think this seems to be the best simple alternative for me:
local function CharacterAdded(char)
repeat wait() until player.Backpack:FindFirstChild("Powerup")
powerup = player.Backpack:FindFirstChild("Powerup")
--tool code here
end
I support you friend, we can do anything we want in our games!
I still think that you added a loop for something which is not required. You will find a better approach on the future :3
You could just do this: Powerup = player.Backpack:WaitForChild("Powerup", math.huge)
WaitForChild() has a second argument, which is the timeout. Am I missing something here?