Racing game placement system bug

How can I fix this bug? If player1 gets past player2 then player1 goes behind player2, it still says player1 is first even though its farther away.

Script:

local function updateScores()
	local scores = {}

	for i, playerName in pairs(racerz) do
		local player = players:FindFirstChild(playerName)

		if player then
			local character = player.Character
			if not character then continue end
			local humanoid = character:FindFirstChildWhichIsA("Humanoid")
			if not humanoid then continue end
			local rootPart = humanoid.RootPart or character:FindFirstChild("HumanoidRootPart")
			if not rootPart then continue end
			local kart = rootPart.Parent:FindFirstChild("Kart")
			if not kart then continue end
			local seat = kart:FindFirstChild("Seat")
			if not seat then continue end
			local checkpointPlayerValue = player:FindFirstChild("CheckpointCount")
			if not checkpointPlayerValue then continue end
			local lapCount = player:FindFirstChild("Lap")
			if not lapCount then continue end
			local placement = player:FindFirstChild("Placement")
			if not placement then continue end

			if table.find(finishedPlayers, player) then
				continue
			end

			local nearestCheckpoint
			local maxDistance = math.huge

			for i, checkpoint in pairs(checkpoints:GetChildren()) do
				local distance = (seat.Position - checkpoint.Position).Magnitude
				if distance < maxDistance then
					nearestCheckpoint = checkpoint
					maxDistance = distance
				end
			end

			if not nearestCheckpoint then continue end

			local playerScore = {
				player = player,
				lapCount = lapCount.Value,
				checkpointCount = checkpointPlayerValue.Value,
				checkpointDistance = maxDistance
			}
			table.insert(scores, playerScore)
		end
	end 

	table.sort(scores, function(a, b)
		if a.lapCount == b.lapCount then
			if a.checkpointCount == b.checkpointCount then
				return a.checkpointDistance < b.checkpointDistance
			else
				return a.checkpointCount > b.checkpointCount
			end
		else
			return a.lapCount > b.lapCount
		end
	end)

	for rank, playerScore in ipairs(scores) do
		local placement = playerScore.player:FindFirstChild("Placement")
		if placement then
			placement.Value = rank
		end
	end
end

Video:

1 Like

Can you recreate the same situation as shown in the video but then after you reverse behind player2 with player1 make sure to put some movement input into player2, just drive forwards a few studs. You might have a problem where the game isn’t picking up that player2 is now ahead because player2 hasn’t technically moved.

I moved player2 and it only updates it when player2 gets to the next checkpoint. I’m currently finding a way. I am close I think

I don’t have think any see of ?

I figured it out, thanks for trying to help :smiley:

2 Likes

never mind, I chose to start over…

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