Infinite Yield when trying to find ImageLabel on PlayerGui

local rs = game.ReplicatedStorage
local moodlesEvent = rs.RemoteEvents.Moodles
local moodles = rs.Moodles

moodlesEvent.hunger1.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local gui = plr:WaitForChild("PlayerGui")
	
	if gui.moodlesMenu.Frame:WaitForChild("hunger") then
		-- Nothing
	else
		local hungericon = moodles.hunger:Clone()
		hungericon.Parent = gui.moodlesMenu.Frame
	end
end)

This script works so that when a remoteEvent is fired, the script first looks if there’s already a copy of the image “hunger”, if not it clones it and parents it to the player’s GUI. However, just as the title suggested, it causes an infinite yield, breaking the script. I’m not sure if it’s a client/server problem or whatsoever, I’m very sorry for my ignorance

WaitForChild won’t resolve until the child exists - so what’s happening here is that it looks for hunger, sees that it doesn’t exist, and stalls. That’s why you’re getting the infinite yield.

What you want is FindFirstChild instead - that one will look to see if it exists and resolves right away, returning either the object or nil. Then the rest of the code should work exactly how you have it

1 Like

Thank you so much!! :>
Sorry again for my lack of knockledge

1 Like

We were all there at one point, and that’s what the help forums are for! Remember - any time you feel stuck at something, we’re here to help, and you’ll get better the more you practice and work at it. Good luck with the rest of your scripting journey!

1 Like

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