What way should I go about making a DataStore for amount of keys collected?

I want to make a game where you collect 64 keys in order to unlock the gate of the mines. The issue is that I don’t know how Im supposed to go about saving how many keys a player has, and deleting the keys that they already got in the game to make sure that they won’t get over 64 key.

How would I go about making this?

Generally the way you format data should be as table so create a dictionary in the table regarding which keys have been collected. For each collected key, create an index of the key name with just a boolean value of true therefore if the keyname is nil then they haven’t collected it, else they have.

If they are unique keys (as opposed to randomly generated) you can save them in an array by a unique identifier like if you set the name to be different for each one you could use that.

local collectedkeys: {string} = {}

-- check if collected
local already_found = table.find(collectedKeys, key.Name) ~= nil

-- collecting a new key
if not already_found then
    table.insert(collectedKeys, key.Name)
end

-- counting keys
local key_count = #collectedKeys

make sure to run some of these examples on the server to reduce client side hackers

2 Likes

Which type of datastore should I use for this operation?

e.x ( ProfileService, Reg DataStore, etc… )

I personally use profileservice and that’s the only open sourced datastore module one I’ve used.

1 Like

Do you know a tutorial on the forum or a introduction of it so I can use it in my game?

There are several tutorials on youtube or elsewhere, also profile service has an api page.

1 Like

Basic Usage - ProfileService If you are more comfortable using normal datastores there’s no problem with using that. SetAsync("Keys", collectedKeys) saving on collection shouldn’t give as many problems as other save-on-close solutions posted here like in the community tutorials pages “Datastores - Beginners to Advanced”

1 Like