How do i save a table in a module?

I don’t nothing about how to write codes in a module, and i want to save the prices for my shop and some kick messages, but i don’t know how to do it, i already searched some threads, but they did helped me, or i didn’t understand. I hope you help me :+1:

3 Likes

The way you’ve explained this is extremely confusing and seems as if you haven’t exactly done much research on it, as there are many other posts with answers to this question. Anyway, from what I can understand, you’re asking to see how you’d save prices for your shop, correct?

Well, to do that you’ll need to use datastores (the word contains a link to a page on Data Stores). However, if that’s not enough, there are several other good, informative videos on Youtube, by people like AlvinBlox. You can find those videos here.

I’d also advise against using module scripts (for the time being) as without a full understanding on them and their uses, you may not use it to it’s best potential. Once you are up to the task of using a module script though, make sure to check out this page on it.

1 Like

Ok, i will explain it further:

I want to make a table, where i can write the shop prices for tools, and kick messages, and put those messages that are stored in the modulescript be the kick message.

Well, basically: I want to know how to use modulescript.

@Benified4Life

1 Like

So if I’m understanding this correctly, you want a ModuleScript to store data such as tool prices and kick messages? That’s pretty simple, and how you’d do that with ModuleScripts would be to return a table with its information being tool prices and kick messages. It’d probably be the easiest to make separate ModuleScripts containing information such as tool prices and kick messages.

This example would be tool prices.

This is the ModuleScript.

return {
    ["ToolName1"] = 170;
    ["ToolName2"] = 260;
}

This would be a regular script.

local ToolPrices = require() -- path to module script

local ToolName1Price = ToolPrices["ToolName1"]
local ToolName2Price = ToolPrices["ToolName2"]

Now, this is a really simple implementation of what I think you want, and you can improve it easily.
Feel free to ask further questions about ModuleScripts if you want to.

Developer Hub Link for ModuleScript’s

Well, i want to store the tool prices in the module script, and i have a shop script which automatically identify the tool name and script:

local prices = {
    ["Doge"] = 350;
    ["TeLaVelGamesHead"] = 200;
	["TheGiggler"] = 10000000;

}

local Rep = game:GetService("ReplicatedStorage")
local event = Rep:FindFirstChild("PetAdd")

event.OnServerEvent:Connect(function(plr, object)
	
    if not prices[object] then return end
    -- car with that name doesn't exist

    if plr.NOOOOOOB.Cash.Value >= prices[object] then
        -- code
    end
end)

The table written in the code is in the script, not the module, well, how do i make this work with the module script without doing it manually?

You require the module.

local prices = require(path_to_module_here)

And then you check the prices from there like you would normally do with a regular table.

Edit:
An example path would be this:

local prices = require(game:GetService("ServerScriptService").ToolPrices)

Thanks, now i have another question, how do i save more tables? for example:

local prices = {
    ["Doge"] = 350;
    ["TeLaVelGamesHead"] = 200;
    ["TheGiggler"] = 10000000;

}

local error001message = "Error 001 ocurred! Try to rejoin the game!"

local banned = {
    ["Noob"];
    ["SomeoneElse"];
    ["OkBoomer123"];
}

Those are tables saved in a normal script, i want to know how do i save them in a module script.

You create new ModuleScripts for error messages and ban messages and then require them, which from there you retrieve data like a normal table.

So, that’s not possible? I want to try to don’t have too much scripts,

You could also have one ModuleScript that has a format like this:

return {
    ["BanMessages"] = {
        ["BanMessage1"] = "Too cool to play this game :)";
        ["BanMessage2"] = "Too bad to play this game :(";
   };
    ["ErrorMessages"] = {
        ["ErrorMessage1"] = "Error message: 666 Demons entered the game";
        ["ErrorMessage2"] = "Error message: 1337 Too epic to enter the game";
   };
    ["ToolPrices"] = {
        ["ToolPrice1"] = 333;
        ["ToolPrice2"] = 420;
   };
}

and in a regular script you’d retrieve it by

local Data = require(path_to_the_module)

local ToolPrices = Data["ToolPrices"]
local BanMessages = Data["BanMessages"]
local ErrorMessages = Data["ErrorMessages"]

Edit: Fixed a grammar error

8 Likes

I got maybe my last question:

How do i get an error message? Right now i want to get ErrorMessage1 for a script. How do i get it?

To get an error message would be pretty easy.

local ErrorMessages = Data["ErrorMessages"]
local ErrorMessage = ErrorMessages["ErrorMessage"]
2 Likes

Maybe I am not understanding how you are doing this?
I am trying to do something like this and am wondering if this is wrong?
local StatsSystem = {}

local StatsTable = {

[‘PistolDamage’] = 23,

[‘RifelDamage’] = 37,

[‘ShotgunDamage’] = 48,

[‘HeadShotDamage’] = 100,

[‘FallDamage’] = 17,

[‘BlastRadious’] = 9,

[‘C4Damage’] = 79,

[‘SprintSpeed’] = 24,

[‘WalkSpeed’] = 16,

[‘Oxygen’] = 100,

[‘Stamina’] = 100,

[‘Health’] = 75,

[‘Hunger’] = 65,

[‘Thirst’] = 65,

}

return StatsSystem

You made a new variable for the items instead of adding it to the main table.

local StatsSystem = {}

StatsSystem.StatsTable = { -- add it to the value you're returning 
[‘PistolDamage’] = 23,
[‘RifelDamage’] = 37,
[‘ShotgunDamage’] = 48,
[‘HeadShotDamage’] = 100,
[‘FallDamage’] = 17,
[‘BlastRadious’] = 9,
[‘C4Damage’] = 79,
[‘SprintSpeed’] = 24,
[‘WalkSpeed’] = 16,
[‘Oxygen’] = 100,
[‘Stamina’] = 100,
[‘Health’] = 75,
[‘Hunger’] = 65,
[‘Thirst’] = 65,
}

return StatsSystem    
1 Like