SetAsync DataStore does not work

My DataStore system doesnt work, the issue is in the SetAsync part but i dont know where, Do you spot anything bad in this script? (I specify that there are no error messages)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
	
	-- Save player money
	MoneyDataStore:SetAsync(player.UserId, player.MoneyPlayer.Dollars.Value)
	
	-- Save player items
	
	for i, ValuesData in ipairs(player.ItemsData:GetChildren())do
	CharacterDataStore:SetAsync(player.UserId, ValuesData.Value)
		
	end
	
	
end)

wait()
	
	if success then
		print("DataSaved")
		
	else 
		print("ErrorDataSaved")
		warn(errormessage)
	end	

end)

Thank you

First to start off, you are spamming the SetAsync method, in which when you try to get data from CharacterDataStore:GetAsync(player.UserId), it’ll just be a ValuesData.Value.
(To make this explanation more simple, let’s say you had 3 values for a character. Name, age, and date of birth. It would set one of these values, not all of them like a table, that you are trying to do.)


Secondly, in-studio, game.Players.PlayerRemoving sometimes doesn’t work, so you’ll have to try legitimately in-game, or any other sort of alternatives, unless it starts working in studio, or it’s been working.


And to your main issue, I don’t see anything wrong prior the code

But you can try the “print debug” method.

1 Like

You’re not saving the inventory items correctly, you keep overwriting the data store value

1 Like

I see, thank you! But how do I save my multiple values?

local data = {}

for i, ValuesData in ipairs(player.ItemsData:GetChildren()) do
	data[ValuesData.Name] = ValuesData.Value -- save the value to the table
end

CharacterDataStore:SetAsync(player.UserId, data) -- save the table

^ You could do this

1 Like

Ok, I replaced the code, but the data is still not saved, I think it was a problem but not the main problem, it’s really strange, But MoneyData save

Can you share the entire script then, you’re very likely not getting data the right way.

Yes I think so too, here is the part of the script that gets the data:

local data
local Moneydata

local success, errormessage = pcall(function()
	
	data = CharacterDataStore:GetAsync(player.UserId)
	Moneydata = MoneyDataStore:GetAsync(player.UserId)
end)



if success then
	print("DataLoaded")
	
	player.MoneyPlayer.Dollars.Value = Moneydata
	
	local values = {}
	

	for i, itemValue in ipairs(player.ItemsData:GetChildren())do
		values[itemValue.Name] = itemValue.Value
	end	
	print (values)
	
	values = data

Hm, you don’t need that loop there for what you’re doing currently, you should be using that to load the character’s data.

What should i use instead the loop to get the data of every values?

What?

If you have a folder for example which keeps these values, you would create a new value inside for every value using a for loop.

Ok i resolved the problem like that :

	for i, itemValue in ipairs(player.ItemsData:GetChildren())do
		itemValue.Value = data[itemValue.Name]

So all the values get the corresponding data,
Thank you everyone