DataStore wont save data

So i made this DataStore code that saves the players characters, this worked completely fine. Now that im trying to also add that the players Coins are getting saved, the whole thing just breaks down without returning any errors. I know in which line of code the problem is, i just dont know how to solve it.

Is there anyone out there that can help me?

This is the code:

local function playerRemoving(player)
	local coins = player.leaderstats.Coins.Value
	local _characters = {}

	local succes, err = pcall(function()
		
		for i,v in pairs(player.CharacterShop.Characters:GetChildren()) do
			table.insert(_characters, v.Name)
			
		end

		task.wait(1)
		datastore:SetAsync(player.UserId.."Characters", _characters)
		datastore:SetAsync(player.UserId, coins)
	end)

	if succes then
		print("Succes!")
	else
		warn("Error")
	end
end

And this is the part causing trouble:

datastore:SetAsync(player.UserId, coins)

Anyone willing to solve this :smiley: ?

try to save everything in a table like this

local function playerRemoving(player)
        local data = {
                      ["Characters"]={}
                      ["Coins"] = player.leaderstats.Coins.Value
                      }
	local succes, err = pcall(function()
		
		for i,v in pairs(player.CharacterShop.Characters:GetChildren()) do
			table.insert(data["Characters"], v.Name)
		end

		task.wait(1)
		datastore:SetAsync(player.UserId, data)
	end)

	if succes then
		print("Succes!")
	else
		warn("Error")
	end
end

image

Thanks in advance, any idea what this means? maybe a typo?

oh I forgot put , after [“Characters”]={} like this [“Characters”}={},

Thanks this works!

If u dont mind, how should i set up the loading part to also load the coins then?

Currently have this and i need to keep the characters part:

	
local _characters = nil

local succes, err = pcall(function()
	_characters = datastore:GetAsync(player.UserId.."Characters")
end)

if succes and _characters ~= nil then
	for i, v in pairs(_characters) do
		local newVal = Instance.new("StringValue")
		newVal.Name = v
		newVal.Parent = ownedCharacters
          
        print("Data has been load succesfully!")

	end
else
	warn(err)

end

end)

DataStore is just really my weakness :sweat_smile:

try this

local data

local succes, err = pcall(function()
	data = datastore:GetAsync(player.UserId)
end)

if succes and data ~= nil then
	for i, v in pairs(data) do
		if i == "Coins" then
                      player.leaderstats.Coins.Value = v
                else
                for z,x in pairs(v) do
                local newVal = Instance.new("StringValue")
		newVal.Name = x
		newVal.Parent = ownedCharacters
                end
                end
        print("Data has been load succesfully!")

	end
else
	warn(err)

end

end)

This works exactly like i wanted thanks! Theres one last error im getting, i dont know if its concerning?

image

oh that is because multiple saves was sent means second one is in queue

is there a way to send fewer requests? so u wont get this message

nah there’s no other ways but you don’t need to worry unless your max players on servers will be like 30-50

Ahh okay, will be max 16 so thats fine. Thanks for all the help and have a nice day! :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.