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.
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.
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!