Jump to content

Delete marker after hitting it


xpnd

Recommended Posts

function seatRumpo(theVehicle, seat, jacked)
    if(getElementModel(theVehicle) == 440 and getTeamName(getPlayerTeam(source)) ~= "Водители") then
        local x, y, z = getElementPosition(theVehicle)
        id = getElementModel(source)
        spawnPlayer(source, x-1, y-2, z, 90, id)
        setCameraTarget(source, source)
        outputChatBox("У вас нет прав для доступа в данное транспортное средство", source)
    elseif (getElementModel(theVehicle) == 440 and getTeamName(getPlayerTeam(source)) == "Водители") then
        outputChatBox("Начинаем работу", source)
        mark1 = createMarker(1104.0576171875, -1740.830078125, 11.5, "cylinder", 8, 255, 69, 0, 150)
        function mark1Func()
            if isElement(mark1) then
                destroyElement(mark1)
            end
            mark2 = createMarker(1167.412109375, -1741.021484375, 11.5, "cylinder", 8, 255, 69, 0, 150)
        end
        function mark2Func(source)
                if isElement(mark2) then
                    destroyElement(mark2)
                end
                outputChatBox("Вы прибыли в точку назначения!", source)   
        end
        addEventHandler("onMarkerHit", mark1, mark1Func)
        addEventHandler("onMarkerHit", mark2, mark2Func)
    end
end
addEventHandler("onPlayerVehicleEnter", getRootElement(), seatRumpo)

I have 2 markers: mark1 and mark2. I need to delete marker, when player hit it. When I hit marker1, marker disappears, but when I hit marker2, nothing happens.

WARNING: work/server.lua:20: Bad argument @ 'addEventHandler' [Expected element at argument 2, got nil]

 

Edited by xpnd
added isElement, warning with destoyElement disappeared
Link to comment
17 minutes ago, ViRuZGamiing said:

function mark1Func()
  if isElement(mark1) then
    destroyElement(mark1)
  end
  mark2 = createMarker(1167.412109375, -1741.021484375, 11.5, "cylinder", 8, 255, 69, 0, 150)
  addEventHandler("onMarkerHit", mark2, mark2Func) 
end

place your event handler in the scope of your variable

outputChatBox works, but mark2 doesn't disappear

Link to comment
  • Moderators
local missionData = {}

function mark1Func(hitElement)
    if getElementType(hitElement) == "player" and missionData[hitElement] then
        local playerMissionData = missionData[hitElement]
        
        removeEventHandler("onMarkerHit", source, mark1Func)
        playerMissionData.mark1 = nil;
        
        local mark2 = createMarker(1167.412109375, -1741.021484375, 11.5, "cylinder", 8, 255, 69, 0, 150)

        playerMissionData.mark2 = mark2
        
        addEventHandler("onMarkerHit", mark2, mark2Func)
        
        if isElement(source) then
            destroyElement(source)
        end
    end
end

function mark2Func(hitElement)
    if getElementType(hitElement) == "player" and missionData[hitElement] then
        local playerMissionData = missionData[hitElement]

        removeEventHandler("onMarkerHit", source, mark2Func)

        if isElement(source) then
            destroyElement(source)
        end

        missionData[hitElement] = nil -- clean up
        
        outputChatBox("Вы прибыли в точку назначения!", hitElement) -- finished? Can't read this...
    end
end

addEventHandler("onPlayerQuit", root,
function ()
    local playerMissionData = missionData[source]
    if playerMissionData then
        if isElement(playerMissionData.mark1) then
            removeEventHandler("onMarkerHit", playerMissionData.mark1, mark1Func)
            destroyElement(playerMissionData.mark1)
        end
        if isElement(playerMissionData.mark2) then
            removeEventHandler("onMarkerHit", playerMissionData.mark2, mark2Func)
            destroyElement(playerMissionData.mark2)
        end
        missionData[source] = nil
    end
end)


function seatRumpo(theVehicle, seat, jacked)
    if(getElementModel(theVehicle) == 440 and getTeamName(getPlayerTeam(source)) ~= "Водители") then
        local x, y, z = getElementPosition(theVehicle)
        local id = getElementModel(source)
        spawnPlayer(source, x-1, y-2, z, 90, id)
        setCameraTarget(source, source)
        outputChatBox("У вас нет прав для доступа в данное транспортное средство", source)
    elseif (getElementModel(theVehicle) == 440 and getTeamName(getPlayerTeam(source)) == "Водители") then
        outputChatBox("Начинаем работу", source)
        
        local mark1 = createMarker(1104.0576171875, -1740.830078125, 11.5, "cylinder", 8, 255, 69, 0, 150)
        
        missionData[source] = {
            mark1 = mark1--,
            -- you can add more data here for the mission
        }
        
        addEventHandler("onMarkerHit", mark1, mark1Func)
    end
end
addEventHandler("onPlayerVehicleEnter", getRootElement(), seatRumpo)

Not tested.

ALSO it doesn't work for multiple players at the same time yet. You will have to spend more time on it to fix that. (this is all I am doing for now)

 

This will be the next step to improve your script:

https://wiki.multitheftauto.com/wiki/SetElementVisibleTo

Edited by IIYAMA
  • 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...