Why this table.sort script work strangely?

---------------Pls off darkmode----------------

local tableToSort = {
{“Player1”, 2};
{“Player2”, 1};
{“Player3”, 3};
}

local sortedTable = table.sort(tableToSort,
function(a,b)
return a[2] > b[2]
end
)

for i,v in pairs(sortedTable) do
print(v[1]…" | "…v[2])
end

Here’s a basic table.sort script used to set ranks some values.

And it works good so I cited this shape to my script.

Here’s what I made. Only one different thing is just values in first(in red square).

Then let’s add print and see the result of this script

Nah, it became idiot. Why it doesnt rank the values from the bigger suddenly?
Somebody help me

p.s - the values what I set are variable.

3 Likes

Well there is a simple answer, you forgot to set; mytab = the table.sort, so your table.sort should look like:

mytab = table.sort(mytab, function(a,b)
    return a[2] >= b[2]
end)
1 Like

mm I changed that too… but I changed because the script worked strangely when return a[2] > b[2].

First Ill do it again like what you advise

You could keep it as >= I didn’t realize I forgot the =

that’s ok
But I found an error message in it

sagras

The 31 line in rauecaranked is this. What does message mean?

I have a table sorting function, and for some reason, i was getting the same issue as you, however i found when i named the things in my table, and then check everything else nearly the same, just fixed it.
example:

local itemtable = {
{["playername"] = "player1", ["Number"] = 5};
{["playername"] = "player2", ["Number"] = 2};
{["playername"] = "player3", ["Number"] = 15};
}
table.sort(itemtable, function(a, b)
	return a.Number> b.Number -- instead of doing a[2] > b[2] i instead did a.Number > b.Number
end)

nooooooo

Here what I changed. Is it right?


: original

With this error message, print result started not to appear suddenly :frowning:


this is line 25.

Remove the mytab redeclaration, table.sort doesn’t return anything

1 Like

What is the classname for: game.Workspace["miridal3:"].Value

her
Remove Here?

actde

Value.Value

No, remove this:

image

1 Like

That isn’t the class name, what type of object value is it? StringValue, numberValue etc

string but it’s not the value to be compared. It shows the username in ‘mirdal3:’.

Now error message died! THX1!!


But bug still alive

Here’s whole editted script. dam what should I do

So is the game.Workspace["miridal3:"].IHHI one a string value too?

I think you may just need to wrap your values with tonumber like tonumber(workspace["mirdal3:"].IHHI.Value)

1 Like

it works TANKYOU THANKYOU TAAAAAAAAAAAANKYOU!!!

I did all the occasion, That All ACCASION WORKED welL! HAHA

Stop using StringValue objects to store numbers. Just use IntValue/NumberValue objects instead.