Make another variable that checks if it already added a lap and create a greater radius it has to leave so it goes back to the beginning situation. I made some alterations:
local sx,sy = guiGetScreenSize()
fontLCD22 = dxCreateFont("lcd.ttf", 22)
local lap = 1
local x, y, z = false
local a, b, c = false
local dist = false
local newLap = false
function updateDisplay()
a, b, c = getElementPosition(localPlayer)
if x and y and z then
dist = getDistanceBetweenPoints3D(x, y, z, a, b, c)
end
if getElementData(localPlayer, "race.checkpoint") == nil then return end
if getElementData(localPlayer, "race.checkpoint") >= 4 then
if dist and dist < 40 then
if newLap == false then
setElementData(localPlayer, "lap", lap+1)
lap = lap+1
newLap = true
end
elseif dist and dist > 50 then
if newLap then
newLap = false
end
end
end
local target = localPlayer
if getElementData(localPlayer, "race.spectating") then
target = getVehicleOccupant(getCameraTarget(localPlayer), 0)
end
dxDrawText("Lap: "..tostring(getElementData(target, "lap")), sx - 90, sy- 155, 40, 30, tocolor(255, 255, 255, 255), 0.6, fontLCD22, "left", "top", false, false, false)
end
addEventHandler("onClientRender", getRootElement(), updateDisplay)
addEvent("onClientMapStarting", true)
function onMapStart()
if getElementData(localPlayer, "lap") ~= 1 then
setElementData(localPlayer, "lap", 1)
end
x, y, z = getElementPosition(localPlayer)
end
addEventHandler("onClientMapStarting", root, onMapStart)
function onClientResStart()
if getElementData(localPlayer, "lap") ~= lap then
setElementData(localPlayer, "lap", lap)
end
end
addEventHandler("onClientResourceStart", resourceRoot, onClientResStart)
This way it only adds once and only triggers again if the player leaves the radius of 50 and goes back to the radius of 40 around the start/finish.