How to loop through everyone's data in my game (Datastore2)

I am planning to migrate everyone’s data in my game to a new format, in order to do this I will have to close down the place and run migration script.

However the problem is that I don’t know how to get everyone’s data and loop through it so I can migrate it to a new format.

I am currently using Datastore2 and I could not find any good example on how to do such a thing, I kind of know how to retrieve data but I will need player’s user id which I don’t have.

What I am specifically asking for is on how to get everyone’s user id and use it to retrieve the data so I can migrate it to a new format. Here is an example:

local userIds = {000000, 000000, 000000, ...} -- I need to somehow get them
for _, userId in pairs(userIds) then -- I need to loop through of all the users
	local userData = Datastore:GetData(userId) -- I will need to get the data of that player
	-- migration part... and etc..
1 Like

A different approach you should consider is to migrate each player individually when they join the game.

1 Like

I am aware of that, and I already have done in the past, and I also made it for next migration but the problem is that I have to keep the migration script and on top of that I will never be able to remove it because if there is a player that haven’t played in the long time and comes back, they will not have their data back.

You can try by getting the id when the player joins add their user id to the table.

local userIds = {}
 table.insert(userIds,#userIds+1, player.UserId)

I do know that, but what I do when I had like over 1K+ play the game and I didn’t get their user-id? Should I just run a loop and guess each player’s user id? Though it would make so many unnecessary datastore calls.

Oh I do not know how to do that maybe using HttpService or something else.

Hey Daw!

One potential solution is essentially running “one and a half” data systems, everytime A Player joins you check if they are a part of the new system, and if they are not then you also check if they are in the old one. If they are in the old one, you can either loop through or hardcode the old values into the template of the new one (A painful process if you hardcode it). After a player is converted, delete their old datastore and key and only save to the new one. You most likely wont ever be able to revert to a single data system unless you’re fine with old players losing their data (or you could give a 30 day window).

Some pseudo code for it would go like this:
On player joining, check for newData, if not newData then check for OldData
If player is found in OldData, Loop through each value (write a function and call it recursively if the value is a table) and set the value of the OldData, to the value for the same Key in newDat
newData[plr]["Inventory"]["Points"] = OldData["Stats"]["Money"]
Or if you have the same data names, your recursive function can search for the key, and change the value
I hope this helps!

1 Like

I already have implemented similar thing, though I am looking for way to migrate every single player data if that’s even possible, most likely yes but I don’t think it’s easy to do especially due to the fact my old datastore script was poorly made.

I would not risk on letting players with old data to lose it due to the fact they have grinded a lot in the game and losing their data would be unacceptable, at least in my opinion.

it’s not possible: Is there a way to dump all my game data from DataStore2 - #5 by Kampfkarren

1 Like

Well thanks for telling me about that, I will use what I am using right now and hope for Roblox to improve their Datastore system which is behind by a lot.