How is this code looping?

ScreenGui.Changed:Connect(function(prop)
	if prop == "Enabled"  and not userInput:IsKeyDown(OpenUiKeybind) then
		
		if ScreenGui.Enabled == true then ScreenGui.Enabled = false return end
		if ScreenGui.Enabled == false then ScreenGui.Enabled = true end

	end

end)
 ā–¶ Maximum event re-entrancy depth exceeded for Object.Changed when calling anonymous function on line 444 in Players.aeroactual.PlayerScripts.Script (x2) 

image
(should only run once)
im missing something but i think its crashing because its looping or something
if it is looping, why is it?

The way you have set it up, it fires for every single change, this is because you used Connect. Unless you call function:Disconnect(), it will keep firing when needed. For your use-case, change Connect to Once; self-explanatory. If you want a limit higher than 1:

local connection
local count = 0
local max = 3
connection = screenGui.Changed:Connect...
    if count >= 3 then
         connection:Disconnect()
         return
    end
    count += 1
    ...
end)
1 Like

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