Wins value don't save (leaderstats saving problem)

I’m trying to make a leaderstats save system for my roblox game. I made it work for my kills counter but, for some reason, the wins counter does not save as well.

I looked for other people having this problem and I got a leaderstats saving script, but it only saves the kills and not the wins

This was the script I used

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local playerDataStore = DataStoreService:GetDataStore("PlayerData")

local function onPlayerAdded(player)
	local success, data = xpcall(playerDataStore.GetAsync, warn, playerDataStore, player.UserId.."_leaderstats")

	if success then
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player

		local Kills = Instance.new("IntValue")
		Kills.Name = "Kills"

		local Wins = Instance.new("IntValue")
		Wins.Name = "Wins"

		if data then
			Kills.Value = data.Kills
			Wins.Value = data.Wins
		end

		Kills.Parent = leaderstats
		Wins.Parent = leaderstats
	end
end

local function onPlayerRemoving(player)
	local leaderstats = player:FindFirstChild("leaderstats")

	if leaderstats then
		xpcall(playerDataStore.SetAsync, warn, playerDataStore, player.UserId.."_leaderstats", {
			Kills = leaderstats.Kills.Value,
			Wins = leaderstats.Wins.Value
		})
	end
end

if RunService:IsStudio() then
	game:BindToClose(function()
		task.wait(1)
	end)
else
	game:BindToClose(function()
		local x, y = 0, 0

		local spawn = task.spawn
		local wait = task.wait

		local function onBindToClose(player)
			onPlayerRemoving(player)

			y += 1
		end

		for _, player in Players:GetPlayers() do
			x += 1

			spawn(onBindToClose, player)
		end

		repeat wait(0) until y == x
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

Just to clarify, I’m testing it on Studio, so I made sure to turn on the API thing!

2 Likes

I might be wrong but in the xpcall in onplayeradded you used

playerDataStore.GetAsync()

when it should be

playerDataStore:GetAsync()

1 Like

It didn’t work. This is what it says:

I don’t know if I should put something between the GerAsync brackets

Try this:


local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local playerDataStore = DataStoreService:GetDataStore("PlayerData")

local function onPlayerAdded(player)
    local success, data = pcall(playerDataStore.GetAsync, playerDataStore, player.UserId.."_leaderstats")

    if success and data then
        local leaderstats = Instance.new("Folder")
        leaderstats.Name = "leaderstats"
        leaderstats.Parent = player

        local Kills = Instance.new("IntValue")
        Kills.Name = "Kills"
        Kills.Value = data.Kills or 0
        Kills.Parent = leaderstats

        local Wins = Instance.new("IntValue")
        Wins.Name = "Wins"
        Wins.Value = data.Wins or 0
        Wins.Parent = leaderstats
    end
end

local function onPlayerRemoving(player)
    local leaderstats = player:FindFirstChild("leaderstats")

    if leaderstats then
        pcall(playerDataStore.SetAsync, playerDataStore, player.UserId.."_leaderstats", {
            Kills = leaderstats.Kills.Value,
            Wins = leaderstats.Wins.Value
        })
    end
end

if RunService:IsStudio() then
    game:BindToClose(function()
        task.wait(1)
    end)
else
    game:BindToClose(function()
        local players = Players:GetPlayers()

        for _, player in ipairs(players) do
            onPlayerRemoving(player)
        end
    end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

It didn’t work either :confused:
Thank you anyways!

Turns out that I was changing the value with a local script and that was why it wasn’t saving. Thank you to the ones who helped anyways!

1 Like

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