Jump to content

Race Lap Counter


koragg

Recommended Posts

So I got a script which counted race laps by counting its checkpoints and when a player reaches a certain cp number, it increases the laps (script had to be manually added to each map for this to work). It goes with no saying that this is just bad for a server resource (as it was made for one map only) so I edited it to work on any map at my server. And the problem now's that it works too good :D

When I go through the start it doesn't increase the lap by 1, but by a lot (random number). How can I cancel this and make it increase lap by 1 each time I pass the start/finish line?

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

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
			setElementData(localPlayer, "lap", lap+1)
			lap = lap+1
		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)

 

Edited by koragg
I know that "onClientRender" is called non-stop and that's why it increases laps by a lot but it's the only most accurate way of counting laps in my opinion.
Link to comment

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.

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...