Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My DataStore's Async isn't working right, why?

Only coins value gets saved. I'm trying to make it so all my values are.

local DSService = game:GetService("DataStoreService"):GetDataStore("SaveStats")
game.Players.PlayerAdded:connect(function(player)
	-- Define variables
	local uniquekey = "id-"..player.userId
	local leaderstats = Instance.new("IntValue", player)
	local coinsvalue =  Instance.new("IntValue", player)
	local gemsvalue = Instance.new("IntValue", player)
	local iqvalue = Instance.new("IntValue", player)
	local tutorial = Instance.new("IntValue", player)
	leaderstats.Name = "leaderstats"
	coinsvalue.Name = "Coins"
	gemsvalue.Name = "Gems"
	iqvalue.Parent = leaderstats
	iqvalue.Name = "IQ"
	tutorial.Name = "tutorial"
	
	-- GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		coinsvalue.Value = GetSaved[1]
		gemsvalue.Value = GetSaved[1]
		iqvalue.Value = GetSaved[1]
		tutorial.Value = GetSaved[1]
	else
		local NumbersForSaving = {coinsvalue.Value, gemsvalue.Value, iqvalue.Value, tutorial.Value}
		DSService:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
local uniquekey = 'id-'..player.userId
local Cointable = {player.Coins.Value}
local Gemtable = {player.Gems.Value}
local IQtable = {player.leaderstats.IQ.Value}
local Tuttable = {player.tutorial.Value}
DSService:SetAsync(uniquekey, Cointable, Gemtable, IQtable, Tuttable)	
	
	
end)

2 comments

0

Any errors in the output window?

0

"Async isn't working right", it's not that it's not working right. It's that you're not using it properly. SetAsync only requires a save key or name, and a value to be saved. https://scriptinghelpers.org/questions/27426/#30949

2 answers

Im not sure if this will work, but it seems that your saving all your values in a table at line 26 then at line 36 you try and save each individual value(which the calue Cointable would override the whole table saved at line 26), which you should try and put them in a table and save that table. like you did with line 26.

try changing line 36 to this

	 local NumbersForSaving = {player.Coins.Value, player.Gems.Value, player.leaderstats.IQ.Value, player.tutorial.Value}
	DSService:SetAsync(uniquekey, NumbersForSaving ) 

This is because the DataStorage will only save the first value after the key. So you need change CoinTable in line 36 to a table and get rid of the values after. It would look like this:

DSService:SetAsync(uniquekey, {
	coinsvalue.Value,
	gemsvalue.Value,
	iqvalue.Value,
	tutorial.Value
})

Also, when you use GetAsync, you would use the values corresponding to the order saved. The code from line 19 to line 24 would set all those values to the saved coin value. Instead, it should look like this:

if GetSaved then
	coinsvalue.Value = GetSaved[1]
	gemsvalue.Value = GetSaved[2]
	iqvalue.Value = GetSaved[3]
	tutorial.Value = GetSaved[4]
else

This discussion is preserved as it was. New answers, comments, and votes are closed.