Setting some random numbers in order from high to low

So what i’m trying to do is set some random numbers from high to low in a table,

its all the players xp in a table and the highest number will add the player to the top of the leaderstats and the lowest will add them to the bottom,

Local AllPlayersXpInOrder = {}

Local XpExample = {35, 42, 35, 29, 52, 35, 25, 45, 14, 45, 23, 63}

Local number = 500
while number ~= 0 do wait()
     for i, v in pairs (XpExample) do
         if v > number then
             table.insert(AllPlayersXpInOrder, #AllPlayersXpInOrder + 1, v)
             table.remove(XpExample, i)
             break
          end
      end
      number = number - 1
end

This is what i have but i am concerned that if a player xp is larger than number then it will not set that large number in order, and if i set number to 1000 for example or more it will take to long to finish setting the table in order

Yeah, this is expensive and does a lot of unnecessary work. Is there a reason why you’ve opted not to use table.sort or were you unaware it existed? You can do all this work with table.sort. As table.sort sorts from least to greatest by default you will need to specify that you want it from greatest to least using a custom sorting function.

local xpTbl = {35, 42, 35, 29, 52, 35, 25, 45, 14, 45, 23, 63}

table.sort(xpTbl, function(a, b)
    return (a > b)
end)

Do note this does not work with dictionaries.

1 Like

This save me a lot of risk and no was not aware of this
Thank you.

check the random page at developer.roblox.com and the Random modules too