Whats the point of adding 'local' before a variable?

Literally all I need to know.

local yes = "hi"
print(yes) -- "hi"

vs.

yes = "hi"
print(yes) -- still prints "hi"

Whats the point of adding ‘local’ befor a variable ???

1 Like

adding the ‘local’ keyword essentially states that the variables is only to be accessed from its current scope.

do
	GlobalVariable = 100
	local LocalVariable = 12
end

print(GlobalVariable) -- 100
print(LocalVariable) -- nil

You can read more about it here

1 Like

What’s funny is that I’ve been scripting for ~6 years yet I’m still cluelesss sometimes.
Another funny thing is that I’ve already know that, however, apparently, inside of a function, it literally does NOT allow you to assign variables unless they’re a ‘local’ keyword.

You can declare global variables inside functions. Keep in mind the variable will be declared when the function itself is called.

local function Hello()
	
	World = 10
	
end

print(World) --Prints nil
local function Hello()
	
	World = 10
	
end

Hello()

print(World) --Prints 10
1 Like

It’s like putting the variables in a order.

do
    Global      = "Hello, World!"
    local Local = "Hello, World!"
end

print(Global) --> "Hello, World!"
print(Local)  --> Exception

Written in here.

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