Saving multiple tools in a datastore

  1. What do you want to achieve?
    I want to see if people own certain tools or they don’t for my game. There is a wide variety of tools (which are shovels in this game) you can purchase.

  2. What is the issue?
    The issue is right now I have a bool value for each tool, and if you own the tool, the bool value becomes true for that specific tool. However, this is inefficient because I have to make a bool value for every single tool, which there is a lot of.

    yes

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried making an array to see if they own the tools, however, this did not work. I attempted many other solutions but this was the only one I could come across that would work.

I don’t think that there is any better way of doing it, because, how else would you store it?

Well, here’s the only other idea:

Instead of making values for everything, just make one when the tool is purchased, and make it a binary value(meaning that there is no actual value). So, to detect that you have the tool, just see if they have the binary value or not.

TL;DR: Only save a value if the player owns it, and save based on an ID system to conserve space.
{ Shovel1 = true, Shovel2 = true, Shovel3 = false}

becomes

{101,102}

and then you can just have an API for tools to IDs

Tools = {
Shovel1 = {101,…}
Shovel2 = {102,…}
Shovel3 = {103,…}
}

function findItemFromId(id)
for i,v in pairs(Tools) do
if v[1] == id then
return i,v
end
end
end

for i,v in ipairs(PlayerDatastoreArray) do
local toolName,toolInformation = findItemFromId(v)
end

Something like this would potentially work. This is just pseudocode though, and could be heavily optimized.

Well, I’d store their data in a dictionary within a UserId keyed dictionary.
Whenever you need that data from another script you could retrieve it via a BindableFunction/RemoteFunction then edit it via a BindableEvent/RemoteEvent.

For example:

local DataDictionary = {}

--// Adding a Player's data to it
DataDictionary[Player.UserId] = {EpicToolName = true}

--// Seeing if they have a tool
if DataDictionary[Player.UserId].EpicToolName then
    --// Code
end

--// Retrieving the data from another script
BindableFunction.OnInvoke = function(Player)
    return DataDictionary[Player.UserId]
end

--// Editing the data from another script
BindableEvent.Event:Connect(function(Player, ToolName)
    DataDictionary[Player.UserId][ToolName] = true
end)

Of course, these dictionaries are possible to be saved/gotten easily.

1 Like

Take a look at this thread that was made a little bit ago. This would probably be the best way for saving a bunch of tools in your case, make a table with the BoolValues to determine if the player owns the item or not.

1 Like

What about duplicated arrays and how would we add the duplicated array or remove it?