Basically, I currently have a simple coin system, theres a folder of around 40-60 collectable coins around the map, all of them named “Coin”.
Once one of them gets picked up, it disappears, and I want to make a datastore system so that once you picked up a coin, you have it collected forever and it doesn’t respawn
Is there any way to make a datastore system like that without necessarily changing the names of the coins, or do I have to go ahead and rename all the coins manually/through a script?
Renaming or assigning each one a unique identifier will be the most reliable solution.
UIDModule.lua.txt (1.9 KB)
Why would you need a module for such a simple task?
I like to do things that way so you can do:
[“a”] = true
[“b”] = false
etc.
And add a tribute to them in workspace with this. this is just easier if you have like thousands of coins
If you just need a way to generate unique IDs, use HttpService:GenerateGUID()
You can just run this through the command line
for _, v in PATH_TO_COINS:GetChildren() do
if v.Name == "Coin" then
v.Name = game:GetService("HttpService"):GenerateGUID()
end
end
Without changing the names, you could save the floored position of each coin as a string
local x,y,z = math.floor(coin.Position.X), math.floor(coin.Position.Y), math.floor(coin.Position.Z)
local coinName = string.format("%i%i%i", x,y,z)
--// Example
local data = {
coinName = false,
}
But a better method would just to be to rename them, which you can run this in the command bar do rename them, but you will have to link the path correctly.
for index, coin in pathToCoins:GetChildren() do coin.Name = index end
You could also use attributes, however the rename/position system would be far more preferable as it’d be far easier to track exactly which coin is which, and it’d allow you to very easily add new coins without having to painstakingly ensure everythings identical to how it was before.
Well you need a way to identify the coin so that your game knows what coin out of the potential 60 coins was collected.
I think the simplest way is to name each coin, a unique name for each.
But alternatively you can keep their names the same, you just need to store a value somewhere on the coin. Like an attribute or a NumberValue as a child.
Or a more complicated system that stores the coins and identifies them on the script side of things, maybe something like
Coins = {}
for i,v in pairs(CoinFolder:GetChildren()) do
Coins[v] = false --CoinDictionary[CoinInstance] = CollectedBoolean
end
Then tie a collect function to each coin, telling the game that when the coin is collected it shouldn’t spawn anymore.
Idk about the datastore side of things I’ve basically never worked with that.
That wouldn’t work in datastores as you can’t store instances, vectors or anything of the sorts. Datastores can only store UTF-8 characters.
Each coin would need a unique index being either a string or number in the table, thus in one way or another the only method to save them would be to essentially rename them.
I’ll also add that simply looping through the coins each time the game runs without renaming them also isn’t a good idea, as they could potentially load in a different order per-server (I’m not too sure how that works though so don’t quote me), and given they do load out-of-order, the data save system would entirely break, as it’d save incorrect coins as having been obtained and vice versa
Yeah I’m too tired to think too hard about this right now.
Possibly this
Coins[i] = {v,false}
I’m pretty sure that works, and if it doesn’t Oh well. But I was thinking it at least associates a coin instance to a number, then a value to know if it’s collected or not. Of course the issue is knowing whether that exact coin is the same coin it was last time someone played the game.
Change the code to find a way to identify the coin somehow (name, child, value, position, etc), though I’d rather it not be by the coin’s name so that ideally the thing works if one day we introduce a coin with a name that doesn’t match the first standard (such as if we named the coins Coin1, Coin2, Coin3, etc then one day we want to add a “SuperCoinExtra”).
Maybe the game can loop through the coins the first time, set an identifier, then store that coin and identifier into datastore (which again I have no experience with so Idek if this works). Then when the server starts up, check for that saved data, loop through the coins, and figure out which coins are the ones saved in the datastore, and set the server’s dictionary of coins.