Best way to store item data in game

Hello everyone,

I’m currently making an inventory system and I’m not sure how to save item data, my code uses those data to determine stuff such as damage and their Picture for example.

I’m Currently Doing this:

local Items = {
   ["Sword"] = {
      Damage = 1,
      Cooldown = 1,
      -- and the list goes on
},
   ["Pickaxe"] = {
       Cooldown = 1,
       Power = 1,
     -- and the list goes on
},

}

As you can see the more items I add the more complex and messy it becomes, is there a better way to do this?

2 Likes

I’d suggest adding a folder to each player that contained all their tools. Inside the folder would be a folder for each tool the player had and inside each tool folder you would put NumberValues to store this data.

Here’s an example of how you would create those in a script:

local CooldownValue = Instance.new("NumberValue")
CooldownValue.Name = "Cooldown"
CooldownValue.Value = 1
CooldownValue.Parent = player.ToolsFolder.Sword

local PowerValue = Instance.new("NumberValue")
PowerValue.Name = "Power"
PowerValue.Value = 1
PowerValue.Parent = player.ToolsFolder.Pickaxe

Edit: I just wanted to add a bit of extra information in case someone reading this didn’t know this but this:

local PowerValue = Instance.new("NumberValue")
PowerValue.Parent = player.ToolsFolder.Pickaxe

can also be written like this:

local PowerValue = Instance.new("NumberValue", player.ToolsFolder.Pickaxe)

That’s because the Instance.new() function takes as a first argument the classType of the Instance being created and as a second argument the Instance that you want to parent this new Instance to

Just thought I’d add this here just in case someone didn’t know

Make sure you save these values with a Datastore each time the player leaves and load them everytime they join

Also make sure to type this code in a serverScript because otherwise the values would only be created to the Client and won’t be able to be saved in datastores

If you want help with Datastore saving I suggest you watch a tutorial on youtube like this one:

I recommend using a ModuleScript for the Item Data. This can be kept from the Client (keeping out exploiters) by keeping it in ServerScriptService. As for Saving, DataStores are the way to go. supergeorge2007 replied with that video, and I think that would be a great way to learn. Good luck, and let me know if you have any more questions.

1 Like

If you want to reduce messiness, you can use separate ModuleScripts required into a parent ModuleScript named Items.

The children would look something like this:

-- ModuleScript named Sword under ModuleScript Items in ServerStorage
return {
  Damage = 1,
  Cooldown = 1,
  --etc
}

Parent would look similar to this:

-- ModuleScript named Items in ServerStorage
local Items = {}

local Children = script:GetChildren()

for _, Obj in pairs(Children) do
  Items[Obj.Name] = require(Obj)
end

return Items

That would return a list where you can access items based on their ModuleScript name, so for the Sword modulescript, to access the items it’d be like so:

local ServerStorage = game:GetService("ServerStorage")
local Items = require(ServerStorage.Items)

print(Items.Sword) -- should print the table containing the damage/cooldown values (in studio only)
1 Like

This is even messier than what the poster was already doing. I don’t recommend this at all.

From what I remember doing this is actively worse for performance than just setting the parent after it is made.

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