Is this data saving code going to work?

local function saveData(player)
	local saveTable = {}
	saveTable.values = {}
	saveTable.values.money = player.money.Value
	saveTable.values.days = player.days.Value
	saveTable.values.daysWait = player.daysWait.Value
	
	ds:SetAsync(player.UserId, saveTable)
end

I don’t want to mess up stuff, but would this work? I know how to call it, so that’s not an issue, but would it work?

2 Likes

Well, obviously it should work. However, by referring to ds, do you mean the service datastore or the datastoreobject?

Yes it should work. You should do the same on GetAsync though! Get the table and set the correct values.

I mean datastoreobject, not the datastoreservice

Ah, then it’s all good. Should work fine.

local function loadData(player)
	local saveTable = {}
	
	saveTable.values = {}
	
	saveTable.values.money = testDataStore:GetAsync(player.UserId, saveTable.values.money)
	saveTable.values.days = testDataStore:GetAsync(player.UserId, saveTable.values.days)
	saveTable.values.daysWait = testDataStore:GetAsync(player.UserId, saveTable.values.daysWait)
	
	testDataStore:GetAsync(player.UserId, saveTable)
end

this is the code i’ve written for loading the data, is this correct?

1 Like

If you’re unsure that your code is going to work or need help with debugging your code, please post it in #help-and-feedback:scripting-support next time.

Your code technically works, but it may not work all the time. If the datastores are down, you won’t be able to handle loading and saving properly which will lead to data loss.

On a side note, using SetAsync to update a player’s data is not a good method to use as this completely overrides the previous data.

If you want to handle datastores efficiently, wrap your code in pcalls, add retries and auto save with coroutines.

No, GetAsync takes one argument. I’m not sure if this will cause an error or ignore the second argument. Also, saveTable will go out of scope when you leave this function, as it’s local (it will go away). Do something like this instead

local playerdata = {}  --an array of save data for every player, put this at the top of the script you are using

local function loadData(player)
	local saveTable = playerdata[player.UserId] 
	saveTable = {}
	saveTable.values = {}
	local LoadedData = testDataStore:GetAsync(player.UserId) 
	saveTable.values.money = LoadedData.values.money
	saveTable.values.days = LoadedData.values.days
	saveTable.values.daysWait = LoadedData.values.daysWait
end

It still needs some more work, as data stores can fail, and if data fails to load and the game thinks the player is new (so their money will start at 0, for example), their data will be overwritten with 0 money and they will lose their data when the game saves to the data store. I recommend you look into pcall and research what can go wrong with Roblox data stores.

Yes it should work fine, it looks good.

Seems like it would, but with issues, maybe try testing it and see how the SetAsync reacts.