Global's And Local's

I am sorry if this is a very basic question, but I am a beginner and am having issues with differentiating between local and global variables. For example:

variable=0
local variable=0
function func()
local variable=0
variable=0
if variable==0 then
local variable=1
end
end

(1) How many separate variables do I have? (2) Which variable is being reassigned in the if statement block? Or is it a new variable separate from all the others? (3) Which variable is the if statement testing for? (4) How do I set a new value to the 1st local variable in the function inside my if statement block? Would that not be just instantiating a new variable limited to the if statement block?

Any help would be greatly appreciated!

Okay lets look at your code:

variable=0                  -- global local variable=0            -- since this is the main chunk, it will be the variable in -- scope for anything at this level                             -- notice how I'm using indents to show the different "blocks" -- or "chunks" of code. At this point, within this .lua file your -- global is inaccessible. function func()     local variable=0 -- This variable is now in scope for this function only and any -- child blocks. For the duration of this function call, the -- "variable" above is no longer accessible.     variable=0 -- Just setting the locally scoped variable to 0, which it already was     if variable==0 then -- Testing the locally scoped variable above, which is 0 so the -- condition is true.         local variable=1 -- This version of "variable" is scoped to code at this indention level -- Anything inside this "if" statement will see variable as 1. You are not -- accessing any of the previously defined variables because you put -- "local" at the front here, making this version only inside the if -- statement.     end end

Okay lets look at your code:

variable=0                  -- global local variable=0            -- since this is the main chunk, it will be the variable in -- scope for anything at this level                             -- notice how I'm using indents to show the different "blocks" -- or "chunks" of code. At this point, within this .lua file your -- global is inaccessible. function func()     local variable=0 -- This variable is now in scope for this function only and any -- child blocks. For the duration of this function call, the -- "variable" above is no longer accessible.     variable=0 -- Just setting the locally scoped variable to 0, which it already was     if variable==0 then -- Testing the locally scoped variable above, which is 0 so the -- condition is true.         local variable=1 -- This version of "variable" is scoped to code at this indention level -- Anything inside this "if" statement will see variable as 1. You are not -- accessing any of the previously defined variables because you put -- "local" at the front here, making this version only inside the if -- statement.     end end