Hi,
I need to get value from var via diferent script, but only thing i know, is that i cant write there local.
I don’t know if this may help, but you can always resort to a value instance like IntValue and NumberValue. This will allow all scripts to interact with it.
I need to share asociative table filed by another tables between scripts
ModuleScripts are perfect for this. Have your ModuleScript return a table of stuff & allow other scripts to consume/edit/whatever that table.
How to do it???
Here is a little example:
ModuleScript
local module = {}
module.Table = {"Foo", "Bar"} -- Adds the table named Table to the module table
return module -- Returns the module table
Script
local ModuleScript = require(script.Parent.ModuleScript)
table.insert(ModuleScript.Table,"Thing") -- Inserts a value to the table in the module script
for i,v in pairs(ModuleScript.Table) do
print(v)
end
That makes more sense, I apologise if I didn’t understand you at the start.
Ok, i thought, that having var in module script is same, like having it in script, that is calling module script. thx
Not sure if this was answered or not, but building off what @Crazyman32 said, I personally do something along the lines of this:
-- Module Script
return(
function()
local tableofvars = {}
tableofvar.Variable = -- whatever value
return tableofvars
end
)()
-- In a Script or Local Script
local module = require(-- wherever module is located)
print(module["Variable"]) -- prints variable defined in module script
I’m not sure if this is the best way to go about doing this but, adds a little OOP - spice to your game which is always a bonus.
i think, that solution i marked is ok, because i exactly need this
Yeah something like that. But you don’t need to wrap it in a function – you can just return a table:
local data = { --[[ stuff ]] }
return data