Problem with leaderstat saving

why won’t this work
the currencies are dances and rebirths respectively

local DSS = game:GetService("DataStoreService")
local leaderstatsData = DSS:GetDataStore("leaderstatsDataStuff")

game.Players.PlayerAdded:Connect(function(player)
    local playerData = leaderstatsData:GetAsync(tostring(player.UserId))
    if playerData then
        player.leaderstats.Dances.Value = playerData.Dances
        player.leaderstats.Rebirths.Value = playerData.Rebirths
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerData = {
        Dances = player.leaderstats.Dances.Value;
        Rebirths = player.leaderstats.Rebirths.Value;
    }
    leaderstatsData:SetAsync(tostring(player.UserId), playerData)
end)
1 Like

GlobalDataStore:SetAsync() requires 3 arguments, while you only supplied 2.
GlobalDataStore:SetAsync(key : string, value : any, userids : Array)
You can also supply 4 arguments:
GlobalDataStore:SetAsync(key: string, value: Variant, userIds: Array, options: DataStoreSetOptions)

Correct me if I’m wrong

Do you have Enable Studio Access to API Services on?

You don’t detect when the server is shutdown.

Code:

local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local leaderstatsData = DSS:GetDataStore("leaderstatsDataStuff")

local function SaveData(player, data)
	local success, errorMessage = pcall(function()
		leaderstatsData:SetAsync(player.UserId, data)
	end)
	
	if not success then
		warn(errorMessage)
	end
end

Players.PlayerAdded:Connect(function(player)
	local playerData = leaderstatsData:GetAsync(player.UserId)
	
	if playerData then
		player.leaderstats.Dances.Value = playerData.Dances
		player.leaderstats.Rebirths.Value = playerData.Rebirths
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local playerData = {
		Dances = player.leaderstats.Dances.Value,
		Rebirths = player.leaderstats.Rebirths.Value
	}
	
	SaveData(player, playerData)
end)

game:BindToClose(function()
	for i, player in Players:GetPlayers() do
		local playerData = {
			Dances = player.leaderstats.Dances.Value,
			Rebirths = player.leaderstats.Rebirths.Value
		}

		SaveData(player, playerData)
	end
end)
2 Likes

You don’t need the 3rd or 4th argument in SetAsync. You just need the key and the data.

Highly recommended, though. It can assist with GDPR racking/removal.

Hello, if the issue revolves around loading the save, try inspecting what the DataStore table looks like. You can use this code to view the table upon loading in the game:

local DSS = game:GetService("DataStoreService")
local leaderstatsData = DSS:GetDataStore("leaderstatsDataStuff")

game.Players.PlayerAdded:Connect(function(player)
	local playerData = leaderstatsData:GetAsync(tostring(player.UserId))
	print(playerData)
	if playerData then
		player.leaderstats.Dances.Value = playerData["Dances"]
		player.leaderstats.Rebirths.Value = playerData["Rebirths"]
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerData = {
		["Dances"] = player.leaderstats.Dances.Value;
		["Rebirths"] = player.leaderstats.Rebirths.Value;
	}
	leaderstatsData:SetAsync(tostring(player.UserId), playerData)
end)

And please show me what result you got.
I also made some changes, it might help.