THE ARGUMENT

Local variables are variables that contain a value within their scope and would normally be nil if used outside of their scope. However, I see a lot of people doing stuff like this and getting away with it (for whatever reason?):

local bool = true
local num = 0

-- When bool and num are used inside the upcoming function they should be nil, but they aren't?

script.Parent.Touched:Connect(function()
	if bool == false then return end
	bool = false
	if num > 10 then return end
	num = num + 1
	wait(3)
	bool = true
end)

Is there a reason why bool and num are, in fact, not nil?

4 comments

2

Because the anonymous function defined inherits the variables above and on its own scope level.

2

Yea, the local variables will get inherited by "lower" scopes, just not the upper. Imagine it like stairs that can only go down

0

Good interpretation ^

0

Additionally, 'bool' will be an upvalue inside of the function. Upvalues are external local variables. And that is used for debounces