I want the placements to be like mario kart but every time I think its working, another bug appears. I have been doing this for HOURS but every time, it breaks.
Here is my code.
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 = nil
local maxDistance = math.huge
local e = nil
local currentCheckpointIndex = nil
local seatPosition = seat.Position
for i, checkpoint in pairs(checkpoints:GetChildren()) do
local checkpointPosition = checkpoint.Position
local distance = (seatPosition - checkpointPosition).Magnitude
e = distance
-- Find the first checkpoint ahead
if checkpointPosition.Z > seatPosition.Z then
if currentCheckpointIndex == nil or distance < (seatPosition - checkpoints:GetChildren()[currentCheckpointIndex].Position).Magnitude then
currentCheckpointIndex = i
end
end
end
currentCheckpointIndex += 1
if currentCheckpointIndex then
nearestCheckpoint = checkpoints:GetChildren()[currentCheckpointIndex]
else
nearestCheckpoint = nil
end
if not nearestCheckpoint then continue end
local playerScore = {
player = player,
lapCount = lapCount.Value,
checkpointCount = checkpointPlayerValue.Value,
checkpointDistance = e,
nearestCheckpoint = nearestCheckpoint
}
table.insert(scores, playerScore)
end
end
table.sort(scores, function(a, b)
if a.nearestCheckpoint == b.nearestCheckpoint then
-- Players are at the same checkpoint, compare distances
return a.checkpointDistance > b.checkpointDistance
else
return a.nearestCheckpoint.Name > b.nearestCheckpoint.Name
end
end)
for rank, playerScore in ipairs(scores) do
local placement = playerScore.player:FindFirstChild("Placement")
if placement then
placement.Value = rank
end
end
end
runService.Heartbeat:Connect(updateScores)
Video of what’s happening.