What is the math behind a rank counter?

Rank Counter:


That is what I am trying to create right now.
This is the hardest thing I am made in my entire scripting career.

Here is what I am trying to do right now:

Players:GetAttribute("LapNumber") + (Players:GetAttribute("DistanceFromCheckpoint") * Players:GetAttribute("SpawnNumber"))

Then I am setting them from least to greatest in a table.

Here are what the Attributes are for:

LapNumber - The lap that the player is on
DistanceFromCheckpoint - The distance (In studs) that the player is away from the next Checkpoint
SpawnNumber - The checkpoint that the player is at. When LapNumber goes up, SpawnNumber resets.

Here is the entire script:

local PlayerTable = {}
local Players: Players = game:GetService("Players")
local ReplicatedStorage: ReplicatedStorage = game:GetService("ReplicatedStorage")

game:GetService("Players").PlayerAdded:Connect(function(Player: Player)
	task.wait(5)
	table.insert(PlayerTable, Player)
end)

Players.PlayerRemoving:Connect(function(Player)
	-- Later
end)

while task.wait() do
	local TableOfPlacements = {}
	for _, Players in pairs(PlayerTable) do
		TableOfPlacements[Players.Name] = Players:GetAttribute("LapNumber") + (Players:GetAttribute("DistanceFromCheckpoint") * Players:GetAttribute("SpawnNumber"))
	end
	table.sort(TableOfPlacements, function(a, b)
		return a > b
	end)
	print(TableOfPlacements)
	for PlayerName, PlacementValue in pairs(TableOfPlacements) do
		print(PlayerName, PlacementValue)
	end
	
	ReplicatedStorage.SendRankTable:FireAllClients(TableOfPlacements)
end

If you can help, please let me know.

Thanks! Bill

1 Like

You can define regions across the track (one per every change in direction/turn), and count how many times a player enters the next region. So say if a lap had 25 regions. As they progress through the first lap, their count will tend from 0 to 25. On the 2nd lap, it will go from 26…50 and so on for the rest of the players. The player with the highest count is in the lead, and naturally you can sort using this from highest to least.

You just need to keep track of each player’s most recent region, and ensure you are incrementing as well as decrementing based on the zone the player enters compared to their previous zone. So if a player has entered their 28th region, and start driving backwards, make sure to decrement their count. I suggest naming each region in sequential order: First out of the start is named “1”, last region of the track is named “25” (Assuming you need 25 regions to fill your track). This way you can compute if the player is going backwards or forwards, and for added functionality tell the player that they’re driving backwards.

You can also accomplish the same functionality with magnitude checks and parts instead of a Region3, if you want a more robust system I would suggest Region3. You could probably also use Zone+ if you wanted, makes the hardest part trivial.

3 Likes

Im still very confused. I don’t know what I am doing at all. Can you give me more details using ZonePlus?

I suggest looking at the documentation and experimenting with the playerEntered and playerExited events, that are also detailed in the zone+ documentation.