So you have _G
which is global variable and _VERSION
. What is it for?
The version of Lua implementation it’s running.
It’d return Luau
(previously Lua 5.1
) if ran on Roblox.
print(_VERSION)
>Lua 5.1
So it’s pretty much useless? Welp thanks for help
It’s probably a bit useless for Roblox devs. It’s useful if you were writing Lua code that might be used outside of Roblox. It would let you change the way you program to know what features might be available. In Roblox, _VERSION
returns Luau
as @Blockzez said.
So if you were trying to make a module that might be used both on Roblox or outside of Roblox, you could use this information to change the structure of your code. For instance, if I wanted to create a function for finding the index of an item in a table, this would look different based on the version since Roblox provides a non-standard library function for it already:
function FindIndex(tbl, item)
if (_VERSION == "Luau") then
return table.find(tbl, item)
else
for k,v in pairs(tbl) do
if (v == item) then
return k
end
end
end
end
In a similar light, you could do this to switch between the feature changes across standard Lua versions too (e.g. later versions of Lua include the bit32
library)
Though you can check if table.find
exists in case you’re in an environment where table.find
is included even if you’re not using Luau.
if table.find then
return table.find(tbl, item)
end
for i, v in ipairs(tbl) do
if v == item then
return i
end
end
return nil
Lua 5.3+ deprecated this and introduced bitwise operators.
Yeah you could switch like that too. But in some cases, you might have a full feature set of implementations and it’s easier to just check the version than every piece.
Building on to all the other posts here, _G means Global,
Here’s a post on that, it really fits well with this!