What Is The Difference Between Global And Local In Coding In Lua?

Hello, this is my first post on here. Anyway, so I have looked through a lot of posts and seen the words local and global a lot when they are showing coding examples. Also, I looked it up, but it didn’t really make sense to me. I want to make my own game, and I’m starting to learn Lua, What’s the Difference?

local variables are only accessible within one script while globals can be accessed by any script (NEVER USE GLOBALS)

1 Like

(inside of only 1 script)

global variables can be used anywhere

local variables can only be used in their own area called a scope, which is everything to the right and inside of the block if proper indentation is used

--cant use test here

if example then --could be a function, loop, do, any of these makes a new block
  local test = 1
  --can use test here because its in the same block (scope)
 end

--cant use test here either
1 Like

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