Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How To Order Gui's With The Highest Amt. of Points on Top - Lowest Points at Bottom?(Attempt posted)

You've seen it everywhere, from the default leaderboards or even your own leaderboards. But I can't fit my mind around how to do it! I tried to use a table and put it in a for loop and go on from there, but my idea didn't work. Can someone help me improve upon my current script?

local plr = game.Players.LocalPlayer
local PlayerLabel = script.Parent.Parent.PlayerLabel -- this is the label used for all players
local Frame = script.Parent

local plrs = game.Players:GetPlayers()

local current = 0
for _,v in pairs(plrs) do
	local pl = PlayerLabel:Clone()
	pl.Parent = Frame
	pl.Name = v.Name

	--[[ so in this loop it creates a gui for every single player in the server. now positioning 
        each gui is where it gets tricky (and also where i am stuck) (and also ordering from least KOs  to most) --]]
	
	prioritytable = {}
	for _, player in pairs(plrs) do
		for _, compare in pairs(plrs) do
			if player.KOs.Value > compare.KOs.Value then
				table.insert(prioritytable,[current + 1]player)
			end		
		end
	end
end

2 answers

✓ Accepted answer

Create a table outside of the loop so that it can be sorted and displayed. Then, iterate through each player's knock outs value and insert it into the table. The script is inserting a table consisting of the score and the player name. Use the sort() function in the table library to sort the values of the table in ascending order. In order to position the labels, you can have a counter representing the y coordinate of each label and increase it by a set amount on every iteration. Note that the script below uses scale to position the labels.

local plr = game.Players.LocalPlayer
local PlayerLabel = script.Parent.Parent.PlayerLabel
local Frame = script.Parent
local plrs = game.Players:GetPlayers()
local KnockOutScores = {}


--Get all the player scores
for _ , v in pairs(plrs) do
	table.insert(KnockOutScores , {v.KOs.Value , v.Name})
end
table.sort(KnockOutScores , function (a , b) return a[1] < b[1] end)

--KnockOutScores would look something like this:
--{{1 , "Player1"} , {3 , "Player2"} , {5 , "Y0_dude"} , {10 , "laughablehaha"}}

--Position the labels
local currentY = 0
local gap = 0.1 --Change if necessary 
local constantX = 0.2 --Example x scale coordinate for each label 
for i , v in ipairs(KnockOutScores) do
	currentY = currentY + gap
	local label = PlayerLabel:clone()
	label.Position = UDim2.new(constantX , 0 , currentY , 0)
	label.Parent = Frame
	label.Name = v[2] --Player name
	label.Text = tostring(v[1])
end


---

1 comment

0

Thank you, this is the answer I've been looking for. (I am not familiar at all with table.sort!)

try using the GUI.position.Y to see the Y axis and then you can compare the Y axis

This discussion is preserved as it was. New answers, comments, and votes are closed.