How do u store variables in module scripts?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to store simple number variables in module scripts.

  2. What is the issue? Include screenshots / videos if possible!
    Well i dont know how.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yeah, only for a couple of minutes though.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You can do something like this:

local newTable = {} -- the table variable will be saved to
local toSave = 3 -- the value of 'toSave' Variable.
local toInsert = {"ToSave", 3} -- {Variable name, Variable Value}
table.insert(newTable, toInsert) -- save 'toInsert' table in 'newTable'
-- getting the variable inside the table --
local value = newTable[table.find(newTable, {"ToSave", 3})][2]

( use @Xitral 's method, its better than this )

1 Like

Store variables like this:

--// Module Script
local MyModule = {}

MyModule.Zombies = 12

return MyModule

or

--// Module Script
local MyModule = {
    "Zombies" = 12
}

return MyModule

Grab variables like this:

--// Script
local MyModule = require(script.MyModule)
print(MyModule.Zombies)

Update variables like this:

--// Script
local MyModule = require(script.MyModule)
MyModule.Zombies = 7

Storing variables in modules is very similar to storing variables in a dictionary. The variables are case-sensitive, so make sure you use the same capitalization when setting and getting the variable.

Hope this helps! :smile:

3 Likes

Oh i see the problem now, rather than mymodule.variablename = something,
I instead immediately did variablename = something. Thanks for the asvice.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.