Jump to content

Сreate a marker and when passed through it delete the old one and create a new one


Ashur

Recommended Posts

Hello, I need to make a race, I started by placing checkpoints through the loop, but I ran into the fact that I do not understand how to correctly delete a checkpoint and then create a new one after the car has passed through the marker
I'm newbie, 3 days in lua and I don't know English well, so I use a translator when creating a topic

Code:

function checkpointHit(hitElement, matchingDimension)
		if getElementType(hitElement)=="vehicle" then 
			return true
		end
end

function Racing( thePlayer )
	local x,y,z = getElementPosition(thePlayer) -- получаем позицию игрока
	x = x - 2 -- прибавляем число 5 к позиции по оси x
	local RaceVehicle = createVehicle ( 411, x, y, z )            -- create a vehicle.
	z, y, x = getElementRotation(RaceVehicle)
	setElementRotation ( RaceVehicle, z,y,x+180)        
    warpPedIntoVehicle ( thePlayer, RaceVehicle )  
	local checkpoints = {  
    { -316.20703125, 1398.3125, 71.81689453125 }, 
    { -410.787109375, 1697.8046875, 39.512222290039 } 
	}
	for k,v in pairs( checkpoints ) do 
		local marker = createMarker ( v[1],v[2],v[3], 'checkpoint', 2.0, 209, 13, 13, 100 ) 
		local blip = createBlipAttachedTo (marker, 0)
	addEventHandler("onMarkerHit", marker, function(hitElement, matchingDimension)
		if(checkpointHit(hitElement, matchingDimension) == true) then 
			destroyElement ( marker )
			destroyElement( blip )
		end
	end)
	end
end

addEventHandler("onMarkerHit", raceStart, function(thePlayer)
	spawnPlayer(thePlayer, -303.2841796875, 1521.22265625, 75.359375)
	fadeCamera(thePlayer, true)
	setCameraTarget(thePlayer, source)
	Racing(thePlayer)
end)

 

Edited by Ashur
Link to comment
  • Ashur changed the title to Сreate a marker and when passed through it delete the old one and create a new one
  • Moderators

Maybe something like that:

local checkpoints = {  
    { -316.20703125, 1398.3125, 71.81689453125 }, 
    { -348.99221801758, 1459.1591796875, 64.749496459961 },
    { -410.787109375, 1697.8046875, 39.512222290039 } 
}

local activeRaces = {}

function nextMarker(thePlayer)
    -- there is an active race
    if activeRaces[thePlayer] then
        -- remove previous marker if exists
        if activeRaces[thePlayer].marker then
            destroyElement(activeRaces[thePlayer].marker)
        end

        -- next marker's index
        activeRaces[thePlayer].checkpoint = activeRaces[thePlayer].checkpoint + 1

        -- this was the last marker -> race finished
        if activeRaces[thePlayer].checkpoint > #checkpoints then
            activeRaces[thePlayer] = nil -- remove race session

            -- you can do anything here, give money to player for example
            outputChatBox("finished")

        else
            -- there is more markers in table, create the next
            local pos = checkpoints[ activeRaces[thePlayer].checkpoint ]
            
            local marker = createMarker(pos[1], pos[2], pos[3], 'checkpoint', 2.0, 209, 13, 13, 100 ) 
            local blip   = createBlipAttachedTo(marker, 0)

            activeRaces[thePlayer].marker = marker

            addEventHandler("onMarkerHit", marker, function(hitElement, matchingDimension)
                if getElementType(hitElement) == "player" and matchingDimension then 
                    nextMarker(hitElement)
                end
            end)
        end
    end
end

function Racing( thePlayer )
    if not activeRaces[thePlayer] then
        local x, y, z = -301.59182739258, 1497.7869873047, 75.678192138672
        setElementPosition(thePlayer, x, y, z)

        local RaceVehicle = createVehicle ( 411, x, y, z )
        setElementRotation ( RaceVehicle, 0, 0, 180)        
        warpPedIntoVehicle ( thePlayer, RaceVehicle )  


        -- save current marker's index in the table
        activeRaces[thePlayer] = {
            checkpoint = 0
        }
        -- create the next marker (the first in this case)
        nextMarker(thePlayer)
    end
end


local raceStart = createMarker(-303.2841796875, 1521.22265625, 75.359375, "checkpoint", 2)

addEventHandler("onMarkerHit", raceStart, function(thePlayer, matchingDimension)
    if getElementType(thePlayer) == "player" and matchingDimension and not isPedInVehicle(thePlayer) then
        Racing(thePlayer) -- start a race
    end
end)

 

  • Thanks 1
Link to comment
20 minutes ago, Patrick said:

Maybe something like that:


local checkpoints = {  
    { -316.20703125, 1398.3125, 71.81689453125 }, 
    { -348.99221801758, 1459.1591796875, 64.749496459961 },
    { -410.787109375, 1697.8046875, 39.512222290039 } 
}

local activeRaces = {}

function nextMarker(thePlayer)
    -- there is an active race
    if activeRaces[thePlayer] then
        -- remove previous marker if exists
        if activeRaces[thePlayer].marker then
            destroyElement(activeRaces[thePlayer].marker)
        end

        -- next marker's index
        activeRaces[thePlayer].checkpoint = activeRaces[thePlayer].checkpoint + 1

        -- this was the last marker -> race finished
        if activeRaces[thePlayer].checkpoint > #checkpoints then
            activeRaces[thePlayer] = nil -- remove race session

            -- you can do anything here, give money to player for example
            outputChatBox("finished")

        else
            -- there is more markers in table, create the next
            local pos = checkpoints[ activeRaces[thePlayer].checkpoint ]
            
            local marker = createMarker(pos[1], pos[2], pos[3], 'checkpoint', 2.0, 209, 13, 13, 100 ) 
            local blip   = createBlipAttachedTo(marker, 0)

            activeRaces[thePlayer].marker = marker

            addEventHandler("onMarkerHit", marker, function(hitElement, matchingDimension)
                if getElementType(hitElement) == "player" and matchingDimension then 
                    nextMarker(hitElement)
                end
            end)
        end
    end
end

function Racing( thePlayer )
    if not activeRaces[thePlayer] then
        local x, y, z = -301.59182739258, 1497.7869873047, 75.678192138672
        setElementPosition(thePlayer, x, y, z)

        local RaceVehicle = createVehicle ( 411, x, y, z )
        setElementRotation ( RaceVehicle, 0, 0, 180)        
        warpPedIntoVehicle ( thePlayer, RaceVehicle )  


        -- save current marker's index in the table
        activeRaces[thePlayer] = {
            checkpoint = 0
        }
        -- create the next marker (the first in this case)
        nextMarker(thePlayer)
    end
end


local raceStart = createMarker(-303.2841796875, 1521.22265625, 75.359375, "checkpoint", 2)

addEventHandler("onMarkerHit", raceStart, function(thePlayer, matchingDimension)
    if getElementType(thePlayer) == "player" and matchingDimension and not isPedInVehicle(thePlayer) then
        Racing(thePlayer) -- start a race
    end
end)

 

ty much

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...