Race placement system - how do I know who is in first, second or third place?

hello
recently i’ve been working on a racing game and one thing i couldn’t figure out how to make was a race placement system, i tried some posts on devforum but none of them could help me

some values ​​are currently stored in the player:

  • CurrentLap - shows the player’s current lap in the race
  • CurrentCheckpoint - shows the player’s current checkpoint in the race
  • CheckpointMagnitude - shows the player’s next checkpoint magnitude

but I don’t know what the script would do to know the placement of all players

Could you help me?

1 Like

Say there are 10 checkpoints in your map. If someone is on checkpoint 5 but lapped once, you could think of a trueCheckpoint value being 15 (10 per lap + current checkpoint 5).

next, you could add a decimal representing the CheckpointMagnitude, for example if someone is halfway between trueCheckpoints 12 & 13, their final placement value would be 12.5.

Now each player has a single value representing their placement, use a sorting algorithim with this and you should have a working system.

2 Likes

and how could i do this in a script? this is being my problem on this system. i have no idea how i could do this in a script

Here’s how you can get the placement value for a given player:
(you’ll have to implement some of the variables yourself)

-- *mapCheckpoints is how many checkpoints are in this map.

-- Checkpoint & lap value.
local trueCheckpoint = currentCheckpoint + currentLap * mapCheckpoints

-- checkpointLength is length between two checkpoints.
local checkpointLength = (currentCheckpointPos - lastCheckpointPos).Magnitude

-- Decimal value representing progress among current checkpoint.
-- It is restricted to always be less than 1.
local checkpointProgress = math.min(checkpointMagnitude / checkpointLength, 0.999)

-- Final placement value.
local placementValue = trueCheckpoint + checkpointProgress
2 Likes

You could add everyone who finishe to a table, and whoever is in first will always be at the top of the table, then just clear the table when you want the next race.

1 Like

could you be more specific? I’m trying a lot of ways, including other posts on devforum but I can’t do it because I need some way to know which player is first. and I tried your system but it just keeps returning 3.999 or 27.999, was it supposed to be like this?

Just to reclarify: the idea behind my approach was it took your variables of currentLap, currentCheckpoint, checkpointMagnitude and converts it into a single variable so you can easily sort each player.


Because the number you’re getting keeps returning a .999 at the end, the checkpointProgress value is not working as intended. It’s supposed to be a number from 0 to 1.
checkProgress
If this image is accurate to how you are getting the magnitude & length variables, then the checkpointProgress formula is:

checkpointProgress = math.max(1 - checkpointMagnitude / checkpointLength, 0)

Now remember the value my system will give you is a player’s basic position in the race. For example, If p1 is at 4.3 and p2 is at 6.8, we know p1 is behind p2.

So we need to sort players from highest to lowest position values, luckily table.sort allows us to easily do this:

-- Player's and their position value (EXAMPLE TABLE).
local sortedPlayers = {
	{PLAYER1, 3.5},
	{PLAYER2, 2.3},
	{PLAYER3, 4.7},
}

-- Sort table from highest to lowest position values.
table.sort(sortedPlayers,
    function(a, b)
        return a[2] > b[2] -- Position A greater than B -> Player A is put ahead of B.
    end
)

-- Now the players are sorted based on their position!

-- sortedPlayers[1][1] is player in 1st place.
-- sortedPlayers[2][1] is player in 2nd.
-- sortedPlayers[3][1] is player in 3rd.
6 Likes

Oh, table.sort(). thank you so much for taking the time to help me.

[quote=“greenboo5, post:4, topic:1590589”]
[/quote] I was searching for a system like this but i cant seem to get it to work. Ive made every variable myself but this is the output im getting, it goes up 1 every checkpoint.
afbeelding

To have a race placement system, you need to create a script that tracks the progress of each player. This script would then compare the progress of each player and assign them a rank based on who has the best time.

First, you need to create a table that stores the name, current lap, current checkpoint, and checkpoint magnitude for each player. This table should be updated each time a player passes a checkpoint or completes a lap.

Next, you will need to create a function that will loop through the table and compare each player’s progress. The function should be able to assign a rank to each player based on their progress. This can be done by sorting the table by the progress of each player.

Finally, you will need to create a GUI to show the players their rankings. This can be done by creating a leaderboard that displays the players’ rankings in order.

By following these steps, you should be able to create a race placement system for your game. Good luck!

If you read through the posts you’ll notice @CavaleiroDev had the same issue where values would end with 0.999 instead of connecting seamlessly. I’m guessing your issue is similar where the checkpointProgress formula is wrong, it should be one of the following depending on what checkpointMagnitude represents:

image

-- checkpointMagnitude is distance from NEXT checkpoint (like the image above)
checkpointProgress = math.max(1 - checkpointMagnitude / checkpointLength, 0)
-- checkpointMagnitude is distance from PREVIOUS checkpoint
checkpointProgress = math.min(checkpointMagnitude / checkpointLength, 0.999)

@BobbieTrooper this is an old post that’s been solved, but @CavaleiroDev never marked it as solved.

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