What's the difference?

Setting a Variable

local Value = AAA

and

Value = AAA

I’ve been writing codes without actually understanding what the differences are, can anyone tell me how one differs to another and which is preferred?

local means it will be shown for a “area” like

if hi then
local bye = "Bye"
end
-- wont be able to use bye

for non local variables its just simple.

First one is defined as a local variable.

While the second one is defined as a global variable.

Local variables are better and more performant.
Here is an example:

local x = 5

local function T1()
   local x = 10
   print(x) -- would print 10
end

print(x) -- would print 5
T1()
x = 5

local function T1()
    print(x-2) -- would print 3
end

print(x-5) -- would print 0
T1()
1 Like

You should probably read through this.

2 Likes