The oldest datastore stored the player’s coins by a number from the leaderstats. Now I changed it so it is saved like a table (added wins) {coins, wins}.
Problem is I want to change the current table to the new table like this {["Coins"] = coins, ["Wins"] = wins} but I’m not sure how to overwrite this over the oldest data (just a number saved in the data). I’m also unsure how you compare it with a table with only numbers {200,25} with a table that has strings.
I tried using update async but no topics really explain how you use it and what the function is for overwriting old data with new.
Both methods store the same data but in a different way. So when a player loads into the game check how the data is stored.
One you get the raw data, just store it using your new method.
The if statement below will run if the datastore only has numerical keys (like your first method of saving data).
local data = ...
if not next(data, #data) then -- table is an array
...
data = newData -- convert the old format of data to the new format
--[[
in your case i imagine it will be something like this:
data = {Coins = data[1], Wins = data[2]}
]]
end
After you do that, just operate with the reformatted data. Your in-game saving system should pick it up if it’s done correctly. If for some you feel like you must manually set the data after reformatting it, just do exactly that and save it via SetAsync or UpdateAsync.
(It is worth noting that you will have to keep this snippet of code in your data system for as long as there exists players with unformatted data.)
This post will help you understand how to use UpdateAsync alongside other practices and features of the DatastoreService.
In a nutshell it returns the next index/key in the table from the one you gave it. If your table has string keys it will start returning the string keys after you have exhausted the numerical keys. This can be used like how I did so above to check if a table contains string keys without having to iterate through the entire table. Its behavior is pretty predictable unless if you have holes in your table (say you have indices [1] and [3] defined, but [2] is nil, [2] would be a hole), in which it will get the next key unpredictably once it passes the hole. The last thing is that you can pass in nil to get the first key in your table.