xxxxxxxxxx
-- Pure Lua:
hello = 2
-- Roblox Lua:
hello = 2
_G.hello = 2
getgenv().hello = 2
xxxxxxxxxx
--Our variable is this
var="a"
--and we cant use it in a function it gives we an error
function abc()
print(var)
end
abc()
--Output: Error
--We can make it a global variable with using _G. method
_G.var2="b"
function cba()
print(_G.var2)
end
cba()
--Output: b
xxxxxxxxxx
--Getfenv used the make normal variables into a global variable
myVariable = "Hello, environments" -- Note: a global variable (non-local)
local env = getfenv()
print(env["myVariable"]) --> Hello, environments
xxxxxxxxxx
--[[
Global variables don't need a declaration like "local." Instead, you'd just
write like this:
]]--
variable = --value
--[[
Note, you don't always have to even assign a value to a variable. You can
use it for the first time and get a value of "nil" which you can update later.
]]--
xxxxxxxxxx
-- roblox luau:
_G.getPlayer = function()
print(game.Players.LocalPlayer)
end
_G.getPlayer()
-- <username>