"Invalid Order Function for Sorting" Error with Player Sorting

Hello,

In my group’s new game, at the end of every round, a sort function is called to see who had the most kills, the most captures, etc. that round, and display it.

However, an extremely rare glitch I’ve seen pop up, is the following:


with this line of code:

local function sortedHighest(stat)
--- Stat is the name of the stat we are looking to sort.
	local player_list = Players:GetPlayers()
	table.sort(player_list, function(p1, p2)
		return p1:GetAttribute(stat) or 0 > p2:GetAttribute(stat) or 0 -- or whatever you want to sort
	end)
	return player_list
end

This is something that is definitely not ran into very often. The ONLY time I’ve noticed this has happened, is when a player leaves at the exact moment that this code is being ran at.

Does anyone know what the issue is here, and how to fix it?

It’s because the or probably isn’t being processed how you expect.

I think what you might intend is this

table.sort(player_list, function(p1, p2)
	return (p1:GetAttribute(stat) or 0) > (p2:GetAttribute(stat) or 0) -- or whatever you want to sort
end)

Which uses parenthesis to make sure that any stat that doesn’t exist is replaced with 0s

1 Like

That was likely the reason. I’m gonna go to bed, and when I wake up in roughly 8 hours, if I see no more reports of this bug, then I know this was the solution, and will mark it as such. Thanks!

Update: This seems like it was, indeed, the solution!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.