What is the difference from a local variable and non-local variable?

Hi! Recently, when i looked at the scripts that my developers created, they do not use the local variable, but at some point they do, but i can’t understand how they differ?

This script:
image

And this one:
image

4 Likes

A local variable is accessible only in the block where it’s declared.
A (non-local variable) is visible to all scopes of a script. In the following code

1 Like

Adding onto this, Roblox terms non-local variables to be called “global” variables. However, do not confuse that meaning with _G, (which many scripters also call global variables,) which are variables that can shared across multiple scripts.

2 Likes

like how @Portal_l1 and @Icee444 said perfectly, for example if the local variable is in a function you can only call that variable within the function, if the variable is not local then you can call the variable from anywhere after setting it’s variable :grinning:

The code below would = nil

function example()
local hiVariable = ("hi")
end

print(hiVariable)

The code below would have an output = hi

function example()
hiVariable = ("hi")
end

print(hiVariable)
3 Likes

If this is a client script, the only thing I can think of for the warn,oldwarn is trying to detect environment scripts which change warn,error,or print ( I cannot think of a single reason why anyone would need to do that, however, so I really don’t know why your developers did that. Maybe it was an old method of detecting environmental scripts? ) Anyways, the Part1 and Part2 could either be the developer changing an already existing variable outside a specific function, or wanting to change the global script environment to confuse exploiters. Either way, this confuses me.

A Non-Local Variable can be accessed anywhere in the script, a Local Variable can be only accessed in the enviroment it was made in. Also Global Variables can be accessed in any Script, here’s an example of a Global Variable: _G["Variable"] = "Hello World!". However they should not be used, mainly because Exploiters can access them, so you should use Modules, RemoteEvents, etc.