I need help with fixing my leaderstats saving script

Issue: The values on the leaderboard don’t save, and I’ve been trying to fix it for like 6 hours already

script below:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DataStore")

local function getLeaderboard(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = leaderstats
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats
	
	dataStore:GetAsync(player.UserId)
end

local function saveLeaderboard(player)
	local theTable = {
		player.leaderstats.Coins.Value,
		player.leaderstats.Wins.Value
	}
	dataStore:SetAsync(player.UserId, theTable)
end

Players.PlayerAdded:Connect(function(player)
	getLeaderboard(player)
end)

Players.PlayerRemoving:Connect(function(player)
	saveLeaderboard(player)
end)

game:BindToClose(function()
	for _, player in game.Players:GetPlayers() do
		saveLeaderboard(player)
	end
end)

I would be grateful if anyone helps

3 Likes

Your not actually setting the values of the objects with the data you get from GetAsync

2 Likes

Thank you, but how could I implement it in the code? for example should I do this below instead or something else?:

local stats = dataStore:GetAsync(player.UserId)
coins.Value = stats[1]
wins.Value = stats[2]

What do you think? Would that be correct and if no then how else should I do it?

1 Like

That would be correct yes although you may want to wrap your get async in a pcall as it could error

2 Likes

Okay, I will wrap the async in a pcall soon, but now I’ll check if that code works and I’ll let you know the result

2 Likes

I changed the code and it still doesn’t save anything…

the current code:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DataStore")

local function getLeaderboard(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	coins.Value = 0
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
	wins.Value = 0
	
	local data
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)
	if success then
		coins.Value = data[1]
		wins.Value = data[2]
	end
end

local function saveLeaderboard(player)
	local theTable = {
		player.leaderstats.Coins.Value,
		player.leaderstats.Wins.Value
	}
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, theTable)
	end)
	
end

Players.PlayerAdded:Connect(function(player)
	getLeaderboard(player)
end)

Players.PlayerRemoving:Connect(function(player)
	saveLeaderboard(player)
end)

game:BindToClose(function()
	for _, player in game.Players:GetPlayers() do
		saveLeaderboard(player)
	end
end)

1 Like

Have you tried printing out the data?

1 Like

when I print data then it shows the coins and wins value, which is zero
image

1 Like

zero because it didn’t save I think

1 Like

Looks like you missed a step…

if success and data then
	coins.Value = data[1] or 0
	wins.Value = data[2] or 0
end

1 Like

Are you changing them in anyway though and if you are what type of script is changing them?

1 Like

Thanks, I changed the code and it still doesn’t save though

1 Like

For now I’m just changing the leaderstats.Coins/Wins value manually while in game, for testing purposes

1 Like

That is as the server right? Your not doing it from the client

1 Like

yes, the script is on the server side
image

1 Like

I meant you changing the values

1 Like

oh wait… let me check… I mean I think I was changing the values in the client actually… xD

1 Like

Hmm, does it matter though? I changed the values in server side now, and still when I rejoined the values were zero
image

1 Like

have you tried printing out “theTable”?

1 Like

Oh, it actually worked, I think it didn’t work for the first time because I tried doing something more with the code but I reverted to the previous code, then checked if works and the stats saved! I will mark this as solution in a moment after I do some more testing, thank you!

2 Likes