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 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)
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!