Undefined variable still working?

The variable Gui_Debounce was never defined in my script, and yet the script still functions just fine. I tried moving this code to another script and it stopped working.
I can’t think of a single explanation. Perhaps the variable is working across scripts because I did define it in another script? But it was a local variable, so how would that make any sense?

The only three instances of this variable found in the script are in this part of the code, so it absolutely has not been defined.

local Open_Inventory = function()
	if Gui_Debounce or Gui_Folder.Skills_Gui.Main_Frame.Visible or Main_Gui.Menu_Frame.Visible or not Stats.Loaded.Value or C_Stats.Target_Npc.Value or C_Stats.Equipping.Value then return end
	Gui_Debounce = true

	Chat_Gui.Enabled = false
	Inventory_Frame.Visible = true
	
	Inventory_Frame:TweenPosition(UDim2.new(0.5,0,0.5,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.3)
	
	Update_Character_Frames()

	wait(0.3)
	
	Gui_Debounce = false
end

This isn’t exactly a problem, but it sure is confusing. Does anybody know how something like this could happen?

1 Like

Do you get any errors in the Output window like Gui_Debounce is nil when the second line comes across it, and after that it’s defined in the third line?
When your script is displayed does it have Gui_Debounce underlined with an error message stating something like ‘variable only used inside the current function, consider making it local’?

Yeah it functions just fine as currently it’s being used as a “Global Variable” located “within this script only”.

It’s the same as defining

local Gui_Debounce = nil

At the top of the script, lua does it for you without erroring which is neat.

Of course that’s not performant (Gotta allocate memory) IDK about performance, but it’s not standard and is pretty confusing so you can just define it the normal way.

local Gui_Debounce = false

There are no error messages at all (not even an underline), as I said before it works perfectly fine.

How come it errors when I move the code to a separate script?

What is the error and what is the line.

Exactly what it should be, the variable Gui_Debounce gets an underline saying that it’s not defined.

Make sure this isn’t happening to you:

And just use local variables like a normal debounce to ensure everything works out.

I think I figured it out. I had two parts of the same code (one for closing the gui and one for opening it). Having two parts of this code removes the error for some reason.
I’ll just define it as a local variable.