How can I create different save files without overwriting others?

I am making a game which has multiple save files, 3 to be exact, for players to save/load their data. I have a time played datastore which is consistent over all save files, plus money and items which is different for each save file. I thought of created a seperate key for each save file, so it would be like:

local key = plr.UserId.."Key"..chosenfile

The thing about that is that it would change the time played only for that save file, and every other save wouldn’t carry the timeplayed. I also thought of created seperate folders and values for each save file and loading them all, and saving them all at the end. The problem with that would be that it would be very inefficent. Is there any way I can save these tables? Also, should I use jsonencode and decode or would it be fine just to save the tables.

2 Likes

If anyone has any advice for this, it would be helpful.

The best to make a players general time played seems to be making a separate value from the load files and start counting up when the players load the file. Save as they leave.

this is quite easy actually you can use the scope of your datastore

--the second argument of DSservice:GetDatastore is the scope, it defaults to "Default" if there is none specified
DSservice:GetDatastore("DatastoreName",ChosentFile):GetAsync(plr.UserId)
--this also works for ordered datastores

here’s the api

Wouldn’t that reset the players played time?

if you have that in the datastore then yes but there’s no way around that besides one big datastore but that could fill up or if it corrupts then they have lost literally everything

So I use 2 datastores? 1 for save files and 1 for the played time?
local temp = {
TimePlayed = 0,
TotalItemsBought = 0,
TotalMoneyEarned = 0,
Money1 = 0,
Money2 = 0,
Money3 = 0,
ItemsUnlocked1 = {},
ItemsUnlocked2 = {},
ItemsUnlocked3 = {},
}
return temp
Thats how my current one looks like.

yeah you could do that but one big datastore is not good in this case

What would be the alternate if I wanted to save those 3 beginning stats and those other slots.

you could only have the time they played on that slot which would be easier or you could have a 4th save that has data like that

I am confused. My code is like: Data = PlrDatastore:GetAsync(Plr.UserId … key.Value) That way, I would have to load less datastore things than before. How would I get the stats?

What I was mentioning is getting the time outside of load saves, if your referring to getting the times for each saved file, that would probably be more easy.
Lets say we have a datastore and we want to add a Time value to that datastore.

game.Players.PlayerAdded:Connect(function(player)
	local TimePlayed = Instance.new("IntValue", player)
	TimePlayed.Name = "TimePlayed"
	TimePlayed.Value = DataStore:GetAsync(player.UserId)
	
	if playerClickedLoadFile then 
		spawn(function() -- So the rest of the code can continue if there is more below
			while true do
				wait(1)
				player.TimePlayed.Value = player.TimePlayed.Value + 1
			end
		end) 
	end
end)

game.Players.PlayerRemoving:Connect(function()
	-- Pcall for saving stats
end)

Of course I didn’t type the whole thing.