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

Why does this error as infinite yield possible?

So i have this server script which works with FE:

function idle_music(action) 
	if action == "add" then
		print("adding")
		local random = lobby_songs[math.random(1, #lobby_songs)]	
		local replicatedStorage = game:GetService("ReplicatedStorage")
		for _, p in pairs (game.Players:GetPlayers()) do
			print("looping through players")
			
			if p:FindFirstChild("PlayerGui"):FindFirstChild("Muusic") then -- checking for screen gui
				print("There already is a musicGui")
			else
				print("There is no musicGui... Adding one rn")
				local musicGui = replicatedStorage:WaitForChild("MusicGui"):Clone()
				musicGui.Name = "Muusic"
				musicGui.Parent = p.PlayerGui
			end
			
			if p and p:FindFirstChild("PlayerGui"):WaitForChild("Muusic"):FindFirstChild("IdleSound") == nil then
				print(p.Name.. " didnt find a sound")
				local sound = Instance.new("Sound", p:FindFirstChild("PlayerGui"):WaitForChild("Muusic"))
				sound.Name = "IdleSound"
				sound.SoundId = "rbxassetid://"..random
				sound.Looped = true
				sound.Volume = 0.4
				sound:Play()
				print("dun")
				print("fine")
			else
				print(p.Name.. " found a sound")
			end
		end
	else
		for _, item in pairs(game.Players:GetPlayers()) do
			if item and item:WaitForChild("PlayerGui", 5):GetChildren() then
				local todel = item:FindFirstChild("PlayerGui"):WaitForChild("Muusic"):WaitForChild("IdleSound", 2)
					if todel then
						todel:Destroy()
					end
				end
			end
	end
end

This is just a function that's called every once in a while and it worked perfectly before but now when I want to add a screen gui to a player gui thourgh a server script it always says "Infinite yield possible in playerName:"WaitForChild(something)"

And it does print "adding" but it never prints "looping through players"

Why does this happen and how can I fix?

1 answer

When Roblox gives the Infinite yield possible... message, that means it sees that WaitForChild may never find what it's looking for, causing the script to yield forever.

In order to combat this, you can either:

\1. Make sure that item will undoubtedly be there.

- OR -

\2. Change the WaitForChild to FindFirstChild and edit your code to work with it accordingly.

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