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

Why wont my table sort function work as intended?

I'm making a friends list ui and it's not working as intended.

Basically, I want it so it sorts the player's friends that are online at the top and the rest who are offline lower at the bottom, but I don't understand how to use table.sort functions or how to use LayoutOrder, it's very very vague on the wiki and I don't understand what they say, they don't even provide examples on it.

Can someone please elaborate on how to use LayoutOrder and table.sort function because it's not clear.

This script keeps erroring at the lines of the table.sort function. I don't know what's wrong with it..

Error: attempt to index field '?' (a nil value)

local friendsPages = game:GetService('Players'):GetFriendsAsync(player.UserId)

local friendsTab = {}
local labels = {}

for i, friends in pairs(friendsPages:GetCurrentPage()) do
	local newLabel = PlaceHolder:Clone()
	newLabel.Visible = true
	table.insert(friendsTab, friends.Username)
	table.insert(labels, newLabel)
	newLabel.Parent = PlaceHolder.Parent
	newLabel.LayoutOrder = i
	newLabel.PlayerName.Text = friends.Username
	PlaceHolder.Parent.CanvasSize = UDim2.new(0, 0, 0.2075 * #friendsTab, 0)
	newLabel.Avatar.Image = 'https://www.roblox.com/headshot-thumbnail/image?userId=' .. friends.Id ..'&width=420&height=420&format=png'
	table.sort(friendsTab, function(a, b)
		return friends[a].IsOnline > b		
	end)
end

6 comments

0

Can you post the error? Why just say "it's erroring" and not post the error.

0

My bad I forgot to add the error. I edited it.

0

What line?

0

I have hundreds of lines of code, I just took a section of the main code for it. I thought stating that the error was occuring because of the table.sort function would suffice, but I guess not. Anyways, it's line 17

0

You're trying to compare a boolean with something that isn't.

0

Well I don't understand how functions work inside table methods, I don't know what the parameters are either.. How do I fix it?

1 answer

Well, you are receiving an error because you are trying to compare a boolean with "b" and "b" hasn't even been defined. The boolean in this case would be the friends[a].IsOnline which returns a true or false based on whether the defined friend is online or not. But looking at the function as a whole, your approach is off and won't get you very far. I'm not going to make the script for you, but I can tell you one possible solution. Just keep in mind I'm not giving an incredible amount of thought to this and my solution is rather inefficent when it comes to latency.

First, you want to sort through all of the player's friends and check to see whether that player is online. If so, then put that player in a table for "OnlineFriends", if the check fails, then put that player in a table for "OfflineFriends". Next when you make your list, you sort through and paste all of the Players in the "OnlineFriends" table first, followed by the "OfflineFriends". For specifics you should refer to the ROBLOX Wiki or, preferably, the ROBLOX Developer Forum.

Hope this helps a little!

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