📜  roblox 局部和全局变量 - Lua 代码示例

📅  最后修改于: 2022-03-11 14:54:55.336000             🧑  作者: Mango

代码示例2
-- A LOCAL variable is accessible only in the block where it’s declared. 
-- Local variables are declared using the local statement:

if true then
    local myNumber = 50
      myNumber = myNumber + 10 -- This will work because is Inside the code Block
end
myNumber = myNumber + 10 -- This wont work because we cannot access it
print(myNumber) -- This wont work also because your code got broken from before

-- A GLOBAL variable looks like this
-- You just take the local part away

if true then
    myNumber2 = 50
    myNumber2 = myNumber2 + 10 -- This will work because is Inside the code Block
end
myNumber2 = myNumber2 + 10 -- This WORKS because it was declared globally
print(myNumber2)