How to fix Infinite Yield Possible?

I’m trying to prevent an Infinite Yield, but I’m unsure how. I checked multiple other topics, but no options worked.

Full Error - Infinite Yield Possible on 'Players.username.PlayerGui.ScreenGui:WaitForChild("ScrollingFrame2")'

local timeout = wait(1)
local ScreenGui = script.Parent.ScreenGui
local ScrollingFrame2  = ScreenGui:WaitForChild("ScrollingFrame2")
Instance:WaitForChild("ScrollingFrame2", timeout)
local Button1  = ScreenGui:WaitForChild("Button1")

Instance:WaitForChild("Button1", timeout)

Button1.MouseButton1Click:Connect(function()
	ScrollingFrame2 = ScrollingFrame2.Visible
end)

Instance:WaitForChild("ScrollingFrame2", timeout)
1 Like

Yielding means your code will come to a halt until a function or an execution finishes. On your 3rd line when you do local ScrollingFrame2 = ScreenGui:WaitForChild("ScrollingFrame2"), it won’t run any code past that line until there’s something called ScrollingFrame2 inside of ScreenGui, infinite yield resulting from ScrollingFrame2 never being in ScreenGui, so while in-game (testing in studio) check to see if you’ve mispelled it, if it’s being placed under ScreenGui at all or if your ScrollingFrame2 is being placed elsewhere. It’s a common mistake for beginners to also try and make changes to a player’s or their own UI through StarterGui instead of the player’s gui.

The global Instance is also a table, and is not userdata or an actual instance therefor it does not inherit the :WaitForChild() method, so that’ll also error. (Unless you’ve change Instance)

2 Likes

I’m not sure if that is a troll post considering the following:

But if this is a genuine post, then make sure that an actual ScrollingFrame named “ScrollingFrame2” is added to your ScreeenGui. If you want it to stop waiting after a certain time, you can supply WaitForChild the second timeout argument as a number, not never

1 Like

isnt timeout supposed to be just a number instead of wait(1)?

Like this?

local timeout = 1

2 Likes

It’s not a troll post, trust me. I’m trying to learn and understand what I’m doing wrong, I’ll try to fix what I did with feedback

1 Like

yes !! ill change that rn, thanks for telling me

Okay, in that case here is a working version of the script, without taking into account your game’s behavior with the child “ScrollingFrame2”

local ScreenGui = script.Parent.ScreenGui
local ScrollingFrame2  = ScreenGui:WaitForChild("ScrollingFrame2", 1)
local Button1  = ScreenGui:WaitForChild("Button1", 1)

Button1.MouseButton1Click:Connect(function() --> I'm guessing this is to toggle the visibility?
	ScrollingFrame2.Visible = not ScrollingFrame2.Visible
end)
1 Like

THANK YOU. It works fine now mostly, sorry if I seemed dumb at first LOL

2 Likes

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