# Help With a Placing System

TL;DR - I need a placing system like Mario Party’s

Hello, I’ve been trying for a while to get this to work right, but now I’m stumped. I’m terrible when it comes to things like this. I’ll try to explain as clearly as possible for little confusion:

For each player there are two values, amount of stars and amount of coins. Places are assigned to each player based upon these values. Stars have priority over coins. And if two or more players have the same amount of coins or stars their places should be the same also.

Ex. Player1 has 5 coins and 2 stars, Player2 has 12 coins and 0 stars, Player3 has 12 coins and 0 stars, Player4 has 18 coins and 2 stars. Player4 is first, Player1 is second, Player 2 and 3 are both third, and there is no fourth place.

I can’t figure out how to set up a system for determining the placing. I have all the values I need, but I don’t know how to set it up. If you’re confused the only thing I can think of like this is how the placing works in Mario Party. Can you guys think of any way to do this?

**[Table.sort

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Table.sort)**

I was trying to use Table.SortByMember[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb], but I still got stumped. :ohdear:

The main problem is mostly just looping through each value and checking to see if it is the same as another value or not. Along with stars having priority over coins.

[editline]4th January 2011[/editline]

After about an hour I finally got it to do placing right with only one of the values, stars. So far I haven’t found any incorrect results. I can easily duplicate it for the coin value, but I still have no clue how to make stars have the priority. :sigh:

What I have so far:

``````

local Place = 1
local PStar = {}
local PPlace = {}

//Temporary Tables
PStar[1] = {}
PStar[1].ID = 1 //Player Index
PStar[1].Amt = 2 //Amount of stars

PStar[2] = {}
PStar[2].ID = 2
PStar[2].Amt = 0

PStar[3] = {}
PStar[3].ID = 3
PStar[3].Amt = 4

PStar[4] = {}
PStar[4].ID = 4
PStar[4].Amt = 2
//

table.SortByMember(PStar,"Amt")

function CheckSame(num)
for i = 1,#PStar do
if i > num and PStar[num].Amt == PStar*.Amt then
return true
end
end
end

for i = 1, #PStar do
PPlace[PStar*.ID] = Place
if not CheckSame(i) then
Place = Place + 1
end
end

PrintTable(PPlace)

``````

Could you explain a bit on how the scoring is done?

Just have a third option in the table for each player, like a total, make this value the number of stars multiplied by 2 plus the number of coins, then sort the players by those values.

This way a star is worth twice the priority of a coin.
You could multiply it by anything, depending on how many coins 1 star is worth.

``````
x = {
{ ID = 1, Amt = 2, Stars = 06 },
{ ID = 2, Amt = 0, Stars = 11 },
{ ID = 3, Amt = 4, Stars = 06 },
{ ID = 4, Amt = 2, Stars = 11 }
}

for k, v in ipairs( x ) do
print( "ID", v.ID, "Stars", v.Stars, "Amt", v.Amt )
end

local function Sort( a, b )
if a.Stars > b.Stars then
return true
end

if a.Stars < b.Stars then
return false
end

if a.Amt > b.Amt then
return true
end

return false
end

print( "====================" )

table.sort( x, Sort )

for k, v in ipairs( x ) do
print( "ID", v.ID, "Stars", v.Stars, "Amt", v.Amt )
end

``````
``````
ID	1	Stars	6	Amt	2
ID	2	Stars	11	Amt	0
ID	3	Stars	6	Amt	4
ID	4	Stars	11	Amt	2
====================
ID	4	Stars	11	Amt	2
ID	2	Stars	11	Amt	0
ID	3	Stars	6	Amt	4
ID	1	Stars	6	Amt	2

``````

table.sort is what you should use in this case, just define your own sort function

I didn’t really understand the whole function thing with table.sort so I never tried it. I combined your code with what I had earlier and it finally works! I haven’t had any incorrect placings yet. Thank you so much, Kogitsune. :iia: