Hi everyone. I have a table with names of players and I want to order them in alphabetical order so they show up in alphabetical order on my custom playerlist GUI.
However, when testing the below script, if the names are very similar (E.G: Player1 and Player2), they are sorted by the time each value was added to the table for each player.
game.StarterGui:SetCoreGuiEnabled("PlayerList",false) local main = script.Parent local player = game.Players.LocalPlayer local curPlayer = main:WaitForChild("CurrentPlayer") local list = main:WaitForChild("Players") local bc = "rbxassetid://93338133" local tbc = "rbxassetid://93338428" local obc = "rbxassetid://93338264" local tempFrame = script:WaitForChild("PlayerTempFrame") game:GetService("ContentProvider"):Preload(bc) game:GetService("ContentProvider"):Preload(tbc) game:GetService("ContentProvider"):Preload(obc) players = {} function updateScrollLength() wait() local y = 0 for _,v in pairs(list:GetChildren()) do y = y + v.AbsoluteSize.Y end local height = (y/list.AbsoluteSize.Y)*.69 list.CanvasSize = UDim2.new(0,0,height,0) end list.ChildAdded:connect(updateScrollLength) list.ChildRemoved:connect(updateScrollLength) function doRefresh() curPlayer.Text = player.Name for i,v in pairs(list:GetChildren()) do v:Destroy() end players = {} for i,v in pairs(game.Players:GetPlayers()) do table.insert(players,v.Name) table.sort(players, function(a, b) return a:byte() < b:byte() end) local frame = tempFrame:Clone() frame.Position = UDim2.new(0,0,0,20 * (#players - 1)) frame.Name = "Player" frame.PlayerName.Text = players[i] if game.Players[players[i]].MembershipType == Enum.MembershipType.BuildersClub then frame.BC.Image = bc elseif game.Players[players[i]].MembershipType == Enum.MembershipType.TurboBuildersClub then frame.BC.Image = tbc elseif game.Players[players[i]].MembershipType == Enum.MembershipType.OutrageousBuildersClub then frame.BC.Image = obc else frame.BC.Image = "" end frame.Parent = list end end doRefresh() game.Players.ChildAdded:connect(doRefresh) game.Players.ChildRemoved:connect(doRefresh)
Is there anyway I can sort the table so Player1 always shows first before Player2 if that situation occurs? I've looked at ROBLOX's playerlist script but that gives me no answers.
Any help is appreciated, thanks!
You're only comparing strings by their first letter!
Interestingly, you don't need to do a:byte() < b:byte()
; this will be the same as a:sub(1, 1) < b:sub(1, 1)
(as in, you can compare strings). You probably want to compare the whole string and not just the first letter, so that would mean you want
a < b
.
That is what table.sort
does by default if you give it no parameters, so this would be sufficient:
table.sort(players)
That is a lexical sort, which is very similar to alphabetical.
If you want alphabetical, you probably want to ignore case (a lexical sort is just looking at the results of :byte()
which do care about capitalization); in that case, do something like
table.sort(players, function(a, b) return a:lower() < b:lower() end)
that is, compare the lowercase (or uppercase) versions of the strings.
Locked by Spongocardo, FearMeIAmLag, EzraNehemiah_TF2, and Redbullusa
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?