There is an issue with my leaderboard update, which sometimes, makes the last members appear on the first places on leaderboard
I dont really understand what makes my code behave like this, it would be really nice if someone will guide me
local DSS = game:GetService("DataStoreService")
local data = DSS:GetOrderedDataStore("Kills")
local RunS = game:GetService("RunService")
local Players = game:GetService("Players")
local TS = game:GetService("TweenService")
local item = game.Workspace.Leaderboard:WaitForChild("Item")
local playerContainer = game.Workspace.Leaderboard.Leaderboard.LeaderboardGui.LeaderboardContainer:WaitForChild("PlayersContainer")
local downscroll = workspace.Leaderboard.DownScroll
local plane = workspace.Leaderboard.Leaderboard
local TWInfo = TweenInfo.new(
.4,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
true)
local properties = {
["Size"] = plane.Size - Vector3.new(0, plane.Size.Y, 0),
["Position"] = plane.Position + Vector3.new(0, 3, 0)}
local properties2 = {
["Position"] = downscroll.PrimaryPart.Position + Vector3.new(0, 6, 0)}
function plrAdded(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local kills = Instance.new("IntValue", leaderstats)
kills.Name = "kills"
local success, plrdata, attempt = nil, nil, 1
repeat
success, plrdata = pcall(function()
return data:GetAsync(plr.UserId)
end)
attempt += 1
if not success then
warn(plrdata)
task.wait(3)
end
until success or attempt == 5
if success then
print("yay connected")
if not plrdata then
print("Assigning default data")
kills.Value = 0
else
kills.Value = plrdata
end
else
warn(plrdata)
plr:Kick("Unable to load your data. Try again later")
end
--while true do -- I guess the problem is here, since it may set too much data
-- task.wait(40)
-- local success, errorMsg = pcall(function()
-- data:SetAsync(plr.UserId, kills.Value)
-- end)
-- if not success then
-- print(errorMsg)
-- end
--end
end
game.Players.PlayerAdded:Connect(plrAdded)
function plrRemoved(plr)
local success, errorMsg, attempt = nil, nil, 1
repeat
pcall(function()
data:SetAsync(plr.UserId, plr.leaderstats.kills.Value)
end)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Saved data successfully for " .. plr.Name)
else
warn("Unable to save for " .. plr.Name)
end
end
game.Players.PlayerRemoving:Connect(plrRemoved)
function onClose()
if RunS:IsStudio() then
return
end
print("Sever has been shut down")
for i, player in ipairs(Players:GetPlayers()) do
task.spawn(function()
plrRemoved(player)
end)
end
end
game:BindToClose(onClose)
--while true do -- It's a script which updates leaderboard
task.wait(40)
TS:Create(plane, TWInfo, properties):Play()
TS:Create(downscroll.ScrollMAIN, TWInfo, properties2):Play()
TS:Create(downscroll.Scroll1, TWInfo, properties2):Play()
TS:Create(downscroll.Scroll, TWInfo, properties2):Play()
local success, pages = pcall(function()
return data:GetSortedAsync(false, 30)
end)
if success then
local entries = pages:GetCurrentPage()
for rank, entry in pairs(entries) do
local cloneditem = item:Clone()
local username = game.Players:GetNameFromUserIdAsync(entry.key)
if playerContainer:FindFirstChild(username) then
playerContainer:FindFirstChild(username):Destroy()
end
if username then
cloneditem.Name = username
cloneditem.NameText.Text = username
end
cloneditem.RankText.Text = rank
cloneditem.CashText.Text = entry.value
cloneditem.Parent = playerContainer
end
end
--end
Main problem contain at the end of the ‘playerAdded’ function and at the end of the whole script I think