Why does this variable keep being 1?

Currently making a racing game and trying to make a lap system. But when i try doing a variable that counts laps it just keeps being one no matter how much laps i drive. Why? Ill mark important parts with a comment.

local function handleFinishLine(player)
	
	local LapsFinished = 0 -- Here
	playerLapData[player].LapsFinished = LapsFinished -- Also here
	local RaceFinished = false
	
	playerLapData[player] = playerLapData[player] or {}

	if not playerLapData[player].lapStarted then 
		resetCurrentLapCheckpoints(player)
		playerLapData[player].lapStarted = true
		print(player.Name .. " starting new lap") 
	elseif playerLapData[player].lapStarted then
		if playerLapData[player].allCheckpointsTouched then
			print(player.Name .. " GoodLap")
			playerLapData[player].LapsFinished = playerLapData[player].LapsFinished + 1 -- Here
			print(player, playerLapData[player].LapsFinished)
			GoodLap:FireClient(player)
		else
			print(player.Name .. " Lap Failed, not all checkpoints touched.")
			InvalidLap:FireClient(player)
		end

You are always setting the value to 0 at the very start of the function. I assume you meant to do that only if there is no data, in which case you should do

local function handleFinishLine(player)
	playerLapData[player] = playerLapData[player] or {
		LapsFinished = 0,
		RaceFinished = false
	}
	if not playerLapData[player].lapStarted then 
		-- Rest of the code continues from here
2 Likes

Now it is just nil for some reason

Output:

Workspace.Car1.Chassis.LapDetectionScript:45: attempt to perform arithmetic (add) on nil and number

How is playerLapData being created? Is it being created somewhere other than in the handleFinishLine function? If so, it’ll need to be modified. Can you please provide a larger sample of code

Yes, it is being created outside the function.

Heres the larger part of the code.

local part1 = script.Parent
local seat = script.Parent.Parent.VehicleSeat
local checkpointFolder = workspace:WaitForChild("Checkpoints")
local finishLine = workspace.Track1.FinishLine

local raceTouchedParts = {}
local playerLapData = {}

local GoodLap = game.ReplicatedStorage.GoodLap
local InvalidLap = game.ReplicatedStorage.InvalidLap
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

for _, part in pairs(checkpointFolder:GetChildren()) do
	if part:IsA("BasePart") then
		raceTouchedParts[part] = false
	end
end

local function resetCurrentLapCheckpoints(player)
	playerLapData[player] = playerLapData[player] or {}
	playerLapData[player].currentLapCheckpoints = {}
	for part in pairs(raceTouchedParts) do
		playerLapData[player].currentLapCheckpoints[part] = false
	end
	playerLapData[player].allCheckpointsTouched = false
	playerLapData[player].lapStarted = false
end

local function handleFinishLine(player)
	
	playerLapData[player] = playerLapData[player] or {
		LapsFinished = 0,
		RaceFinished = false
	}
	
	if not playerLapData[player].lapStarted then 
		resetCurrentLapCheckpoints(player)
		playerLapData[player].lapStarted = true
		print(player.Name .. " starting new lap")
	elseif playerLapData[player].lapStarted then
		if playerLapData[player].allCheckpointsTouched then
			print(player.Name .. " GoodLap")
			playerLapData[player].LapsFinished = playerLapData[player].LapsFinished + 1
			print(player, playerLapData[player].LapsFinished)
			GoodLap:FireClient(player)
		else
			print(player.Name .. " Lap Failed: Not all checkpoints touched.")
			InvalidLap:FireClient(player)
		end

		local connection
		connection = RunService.Heartbeat:Connect(function()
			resetCurrentLapCheckpoints(player)
			playerLapData[player].lapStarted = true
			print(player.Name .. " Lap Reset - New Lap Started")
			connection:Disconnect()
		end)
	end
end