LuaDesignJoin Date: 2010-05-24 Post Count: 1443 |
Say I want to share a table throughout the server scripts. Is _G not a good way to go or is there a different method?
If I had a penny for every penny I had... Mother of god. |
|
iiNemoJoin Date: 2013-07-22 Post Count: 2380 |
Just don't use global variables at all.
Fish Are Friends, Not Food |
|
LuaDesignJoin Date: 2010-05-24 Post Count: 1443 |
Then how would I share a global table? I'm looking for advice not a do not.
If I had a penny for every penny I had... Mother of god. |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
_G is fine but the problem is all scripts can access it, which may not be a problem now but if a later script decides to mess with _G it can potentially ruin things.
ModuleScripts are a cleaner solution mainly because you only have access to what it returns when you require it, otherwise you don't. |
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
Sure, it's absolutely fine if you know how to use it.
People who say "never use global variables ever for any reason whatsoever forever and ever amen" typically don't know everything about them, or how to manipulate them safely. |
|
76kmJoin Date: 2015-08-14 Post Count: 5455 |
@op
if you don't want to transfer a global table, you could make the individual scripts generate a table whenever required rather than just have a table strung everywhere?
idk, inefficient suggestion, but. |
|
LuaDesignJoin Date: 2010-05-24 Post Count: 1443 |
So could I do this?
-- Module
local module = {};
module.var = 42;
return module
-- Script A
local module = require(module)
module.var = 1000;
table.insert(module, "ok")
-- Script B
local module = require(module)
print(module.var)
for i,v in pairs(module) do
print(v) -- Would print 1000 and ok
end
If I had a penny for every penny I had... Mother of god. |
|
iiNemoJoin Date: 2013-07-22 Post Count: 2380 |
_G on the server stays on the server and cant be accessed by the client, _G on the client stays on the client and cant be accessed by the server
Fish Are Friends, Not Food |
|
|
iiNemoJoin Date: 2013-07-22 Post Count: 2380 |
Just used Bindable Events/Functions
Fish Are Friends, Not Food |
|