So I wanted to make a leaderboard system for a game, problem is, it does work until the second player gets a higher score then the first one, the second player goes to one, but the first player doesn't go to 2? It's a server script inside workspace (inside a folder) I commented the datastores because I work in team create and it bugs out :/ The problem is in function UpdateStats I believe.
local DataVersion = 1
local DataStore = game:GetService("DataStoreService")
local LeaderboardSave = DataStore:GetDataStore("LBS_"..DataVersion)
local key = 1902 -- Do NOT change! Change dataversion if you want to reset
LeaderBoard = {}
LeaderVals = {}
function UpdateSave()
LeaderBoard = {}
LeaderVals = {}
-- local NC = LeaderboardSave:GetAsync(key)
local NC = {'empty', 'empty', 'empty', 'empty', 'empty', 'empty', 'empty', 'empty', 'empty', 'empty',}
local LV = {100, 10, 9, 8, 7, 6, 0, 0, 0, 0}
for index, save in pairs(NC) do
table.insert(LeaderBoard,save)
end
for index, save in pairs(LV) do
table.insert(LeaderVals,save)
end
-- LeaderBoard:SetAsync(key,LeaderBoard)
end
function replaceStats(plr, num)
LeaderBoard[num] = plr.Name
LeaderVals[num] = plr:WaitForChild('Stats'):WaitForChild('NetWorth').Value
print('replaced '..num)
end
function UpdateStats()
wait(0.1)
for index, plr in pairs(game.Players:GetPlayers()) do
local net = plr:WaitForChild('Stats'):WaitForChild('NetWorth')
if LeaderVals[1] < net.Value then
replaceStats(plr, 1)
elseif LeaderVals[2] < net.Value then
replaceStats(plr, 2)
elseif LeaderVals[3] < net.Value then
replaceStats(plr, 3)
elseif LeaderVals[4] < net.Value then
replaceStats(plr, 4)
elseif LeaderVals[5] < net.Value then
replaceStats(plr, 5)
end
end
end
function DisplayStats()
local TLs = workspace:WaitForChild('HighStats'):WaitForChild('Main'):WaitForChild('SurfaceGui')
TLs.Player1.Text = LeaderBoard[1]..': '..LeaderVals[1]
TLs.Player2.Text = LeaderBoard[2]..': '..LeaderVals[2]
TLs.Player3.Text = LeaderBoard[3]..': '..LeaderVals[3]
TLs.Player4.Text = LeaderBoard[4]..': '..LeaderVals[4]
TLs.Player5.Text = LeaderBoard[5]..': '..LeaderVals[5]
TLs.Player6.Text = LeaderBoard[6]..': '..LeaderVals[6]
TLs.Player7.Text = LeaderBoard[7]..': '..LeaderVals[7]
TLs.Player8.Text = LeaderBoard[8]..': '..LeaderVals[8]
TLs.Player9.Text = LeaderBoard[9]..': '..LeaderVals[9]
TLs.Player10.Text = LeaderBoard[10]..': '..LeaderVals[10]
end
while wait(1) do
UpdateSave()
UpdateStats()
DisplayStats()
end
