Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a "top ten leaderboards [SG]" datastored?

Asked by 10 years ago

How could i make this work datastored so its not the top ten players in the server?

function Create()
    return script.Parent.PlayerFrame:Clone()
end

function GetTopPlayers()
    local TopPlayers={}
    local Numbers={}
    local PossiblePlayers={}

    local function GetPlayerWins(Player)
        if Player and Player:FindFirstChild("leaderstats") and Player.leaderstats:FindFirstChild("KOs") then
            return Player.leaderstats["KOs"].Value
        end
        return 0
    end

    for i,v in pairs(Game.Players:GetPlayers()) do
        table.insert(Numbers,GetPlayerWins(v))
        table.insert(PossiblePlayers,v)
    end

    local function GetBiggest()
            local Biggest=math.max(unpack(Numbers))
            local Player

            for index,value in pairs(PossiblePlayers) do
                if Biggest==GetPlayerWins(value) then
                    Player=value
                    table.remove(PossiblePlayers,index)
                    break
                end
            end
            for i,v in pairs(Numbers) do
                if v==Biggest then
                    print("Removed: "..v.." "..Numbers[i])
                    table.remove(Numbers,i)
                    break
                end
            end
            return Player
    end

    for i = 1,#Game.Players:GetPlayers() do
        local Biggest=GetBiggest()
        table.insert(TopPlayers,i,Biggest)
    end
    return TopPlayers
end

function Update()
    local TopPlayers=GetTopPlayers()
    local function Clear()
        for i,v in pairs(script.Parent:GetChildren()) do
            if v.Name=="Player" then v:Destroy() end
        end
    end

    Clear()

    if #TopPlayers>0 then
        for i,v in pairs(TopPlayers) do
            if i>10 then break end
            pcall(function()
            local Frame=Create()
            Frame.BackgroundColor3=Color3.new(1,1,1)
            Frame.Name="Player"
            Frame.Parent=script.Parent
            Frame.Position=UDim2.new(0.05,0,i*.08,0)
            Frame.PlayerName.Text=v.Name
            Frame.Place.Text="#"..i
            Frame.Wins.Text=tostring(v.leaderstats["KOs"].Value)
            end)
        end
    end
end

Game.Players.PlayerAdded:connect(function(Player)
    Update()
    Player:WaitForChild("leaderstats"):WaitForChild("KOs").Changed:connect(function()
        Update()
    end)
    Update()
end)

Game.Players.PlayerRemoving:connect(function(Player)
    Update()
end)

1 answer

Log in to vote
0
Answered by 10 years ago

It may work, if you test it on your script and put it in your game ( The game you want it to be in ) And edit it if it doesn't work or if it works, Its usable.

Ad

Answer this question