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

How to Auto-Sort Teams? [Solved]

Asked by 10 years ago

Could someone help me think through the logic for such a script? A current script I've written sorts players into teams purely by going down the alphabetical list of players, but what if I want to sort players into teams based on a particular leaderstat value, such as points? My biggest problem, I think, is knowing how to compare all of the players values to know which ones are the highest and need to be separated out accordingly. I'm guessing I have to create a table where the script cycles through comparing a given player's points to the highest person it's found so far, and place them in the particular key of the table, bumping down any other players below as needed. That seems like a VERY repetitive process though.

2 answers

Log in to vote
1
Answered by
SanityMan 239 Moderation Voter
10 years ago

As you said, using a table would probably work. If you use a for loop, it might be less redundant than you would think.

local g = game.Players:GetChildren()
for i = 1, #g do --or another player sort table you want to use
--your code checking their stat variables
--you might be able to compare things with an if statement like
if g[i].yourvakue >= --[[your table #1 value]] then
table.insert(your table info)
end
end

I would recommend using the player names as keys in the table, and the stat as the value.

0
Ok that makes sense, though what if the second ranked player's name starts with Z? the script would come to them last and would have to force a spot between the first and third players deaththerapy 60 — 10y
0
Oh, wait I see. That's what using '.insert' prevents, nevermind. One other question though, if I do use playernames for the keys could I still call each value by table[1]? Or would I have to know the player's name and call the value by table["PlayerName"]? deaththerapy 60 — 10y
0
I think you would need the player's name to index the table. I'm not sure, but using a dictionary table might help with organization. I will go check some of my scripts, there is one method I used once that might help. SanityMan 239 — 10y
1
Ok, so with table.insert, you can do the following: table.insert(yourtable, position, {data}). Where data is, you can define multiple parts of your table. I would use this in your case for something like table.insert(table, 1, {name = player.Name, stat = however-you-store-your-stats}). Hope it helps you! SanityMan 239 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

Woww, I split my head too much over this, but I think I finally came up with a solution. Thanks SanityMan for pointing me in the right direction. As an overview, the function starts with an empty table and a table returned by game.Players:GetChildren(). It then inserts the first player into the empty list as a test value. Then for each player in the game.Players:GetChildren() list, it compares the players' Points Values until it finds the player in the once-empty list whose Value is less then the current player being checked. It then inserts the current player into the once-empty-player's spot in the list, bumping him and all others below down one.

local p = game.Players:GetChildren()
local plr = {
}

function sorter()
    local pl = game.Players:GetChildren()

    plr = {} -- Clear out previous values in case of reuse
    table.insert(plr, 1, pl[1]) -- Insert the randomly first player

    for x = 1, #pl do
        for i = 1, #plr do
            if pl[x].leaderstats.Points.Value > plr[i].leaderstats.Points.Value then
                table.insert(plr, i, pl[x]) -- Is new player higher than old? replace his spot. Else try next spot
                return
            end
        end
    end
end

If anyone wants to use this or point out an error, feel free!

0
The great thing about this function is that you can easily swap out the Points leaderstat with any numerical Value or Property attached to the Player. Simply replace both of the (pl[x].leaderstats....) parts on line 13. deaththerapy 60 — 10y

Answer this question