How to print who is in 1st, 2nd, 3rd, 4th, 5th etc

If I have 10 people racing towards a part.
How do I print the players name, and there position against the other players?

EXAMPLE SITUATION

Player1 is closest to the part
Player2 is second closest to the part
Player3 is farthest from the part

OUTPUT

Then I want it to print

Player1Name, 1st
Player2Name, 2nd
Player3Name, 3rd

I need it to go up to 20 people, so would it be any different than just three?
It needs to constantly update as there position/closeness to the part changes

Thank you!

First, you’re gonna need to get the position of every player’s HumanoidRootPart in their character. Then, get the position of the part that you are racing towards and use .Magnitude to find the distance.

Next, put these magnitude values in the table, sort the table from the smallest to greatest value, and then print out the names.

1 Like

Could you please elaborate on the table and printing part, this is what im really confused on.?

i would do smth like this
use a timer module when the race starts
when the player touches the end mark the timer and print the players name with the time

1 Like

You use the table.sort function to sort a table from least to greatest. The DevHub doesn’t have a great explanation on it, but @EmbatTheHybrid does a really good job explaining how to use table.sort in this post:

Here a table sorting function I made to sort random numbers from least to greatest:

local t = {15,7,66,93,10}

table.sort(t, function(a,b)
	return a < b
end)

print(t)
2 Likes

To do it simpler if wanted, if you’re wanting a least to greatest/ascending order sort, you don’t even need to give a function as the default sorting uses <, but if you wanted to do a descending sort, you’d need to make a function

Also thanks for mentioning my explanation haha

1 Like

Wow, I never knew returning values from tables where that simple! Thank you so much!
And to you too @EmbatTheHybrid

1 Like

But is there a way to keep the players name attached to the value?

Or does this already happen?

And I know I’m kinda asking for a bit, but how do you get each individual position by itself? A

My example is that the person in first has a name tag in there character, which I want to assign “1st Player1Name” as the text value. And for second “2nd Player2Name”.

How can I complete this part?

Think what you can do after getting the positions of each player and sorting to get the top 3, you can reference their character and change the tag there

This will require you to store a key value dictionary in the table, where the key is the character and the value is the distance

So say you have 1 player who is 10 studs away from soemthing, their entry in the table would be

{key = workspace.Player1, value = 10}

Then in the table.sort, you need to reference that

table.sort(t, function(a,b)
	return a.value < b.value
end)

To sort properly, then just reference the key entry to get the character to change their name

Sorry if my explanation was a bit weird, I’m having a slight headache right now but still will try to help

2 Likes

Yeah sadly I have no idea what any of this means or what I should do with this :joy:.

At this point I should just hire a Scripter…

Don’t let me bother you, get some rest or something.

Thanks for your help though, I’m sure the information I’ve gathered today will help me in the future!

1 Like