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: