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

Sorting table from highest to lowest?

Asked by
Sxerks3 65
7 years ago
Edited 7 years ago

I'm attempting to write a script where it takes the table and sorts out the lap number from highest to lowest. The thing is, it correctly prints out the first player, but the players afterwards bug out.

Table script:

local allPlayers = game.Players:GetPlayers()

table.sort(allPlayers, function(a, b)
    return a.Lap.Value > b.Lap.Value
end)

while wait(0.001) do
    for i,v in next, allPlayers do
        print(v.Name .. ": " .. i .. "th")
    end
end

PlayerInstance script:

local allPlayers = game.Players:GetPlayers()
local test = game.Players.LocalPlayer:GetFullName()

for _, player in pairs(allPlayers) do
    if player.Character then
        if  test ~= "Player.Sxerks2" then
            -- was only a test
            local lapNum = Instance.new("IntValue", game.Players.LocalPlayer)
            lapNum.Name = "Lap"
            lapNum.Value = 2
        end

        local lapNum = Instance.new("IntValue", game.Players.LocalPlayer)
        lapNum.Name = "Lap"
        lapNum.Value = 1

        local checkpoint = Instance.new("IntValue", game.Players.LocalPlayer)
        checkpoint.Name = "Checkpoint"
        checkpoint.Value = 0
    end
end

Thanks!

0
What is this "table?" Is it part of the leader board? Or is it an object. If it is a part, then you need to put workspace.table marioblast1244 113 — 7y
0
Wait... what!?? table.sort? Has that always been a thing? NewVoids 97 — 7y
0
@NewVoids Yeah, lol. It sorts data in a table by numerical order, then alphabetical order. TheeDeathCaster 2368 — 7y

1 answer

Log in to vote
-1
Answered by 7 years ago

Table.sort()

C:

Ad

Answer this question