Jump to content

Problem destroyElement


(s)ection

Recommended Posts

ha, i have a problem when the player exit of the vehicle the marker and the blip don't destroy help please :fadein:

function OnEnter ( thePlayer, seat, jacked ) 
    if ( getElementModel ( source ) == 448 ) then  
outputChatBox ( "Job started !",player,255,255,0 ) 
local am = createMarker(-2075.11133,900.70227,64.13281 - 1, "cylinder", 3, 0, 102, 51, 153) 
local ab = createBlip (-2075.11133,900.70227,64.13281, 0, 3, 102,51,153 ) 
    end 
end 
addEventHandler ( "onVehicleEnter", getRootElement(), OnEnter ) 
  
function OnExit ( am ,ab ,thePlayer, seat, jacked ) 
    if ( getElementModel ( source ) == 448 ) then  
    destroyElement( source ) 
outputChatBox ( "You quitted your job.",player,255,0,0 ) 
        destroyElement ( ab ) 
        destroyElement ( am ) 
    end 
end 
addEventHandler ( "onVehicleExit", getRootElement(), OnExit ) 

Link to comment
function OnEnter ( thePlayer, seat, jacked ) 
    if ( getElementModel ( source ) == 448 ) then 
outputChatBox ( "Job started !",player,255,255,0 ) 
am = createMarker(-2075.11133,900.70227,64.13281 - 1, "cylinder", 3, 0, 102, 51, 153) 
ab = createBlip (-2075.11133,900.70227,64.13281, 0, 3, 102,51,153 ) 
    end 
end 
addEventHandler ( "onVehicleEnter", getRootElement(), OnEnter ) 

Try this.

Link to comment
function OnEnter ( thePlayer, seat, jacked ) 
    if ( getElementModel ( source ) == 448 ) then 
        outputChatBox ( "Job started !",thePlayer,255,255,0 ) 
        am = createMarker(-2075.11133,900.70227,64.13281 - 1, "cylinder", 3, 0, 102, 51, 153) 
        ab = createBlip (-2075.11133,900.70227,64.13281, 0, 3, 102,51,153 ) 
    end 
end 
addEventHandler ( "onVehicleEnter", getRootElement(), OnEnter ) 
  
function OnExit ( thePlayer, seat, jacked ) 
    if ( getElementModel ( source ) == 448 ) then 
        destroyElement( source ) 
        outputChatBox ( "You quitted your job.",thePlayer,255,0,0 ) 
        destroyElement ( ab ) 
        destroyElement ( am ) 
    end 
end 
addEventHandler ( "onVehicleExit", getRootElement(), OnExit ) 

Link to comment
function MarkerHit4 ( element ) 
    if (getElementType(element) == "player") then 
        outputChatBox ( "[You win 1034$ !", element, 255, 255, 0 ) 
        givePlayerMoney ( element, 1034 ) 
    elseif (getElementType(element) == "vehicle") then 
        local player = getVehicleOccupant(element) 
        if (player) then 
            outputChatBox ( "[You win 1034$ !", player, 255, 255, 0 ) 
            givePlayerMoney ( player, 1034 ) 
        end 
    end 
end 
addEventHandler( "onMarkerHit", am, MarkerHit4 )  

Link to comment
function MarkerHit4 ( element ) 
    if (getElementType(element) == "player") then 
        outputChatBox ( "[You win 1034$ !", element, 255, 255, 0 ) 
        givePlayerMoney ( element, 1034 ) 
    elseif (getElementType(element) == "vehicle") then 
        local player = getVehicleOccupant(element) 
        if (player) then 
            outputChatBox ( "[You win 1034$ !", player, 255, 255, 0 ) 
            givePlayerMoney ( player, 1034 ) 
        end 
    end 
end 
addEventHandler( "onMarkerHit", am, MarkerHit4 )  

Sorry but it's don't work :S

Link to comment

Huh, You mean it ?

[13:49:09] WARNING: editor_test\server.lua:52: Bad argument @ 'getElementModel' [Expected element at argument 1, got number '448']

[13:49:09] WARNING: editor_test\server.lua:96: Bad argument @ 'addEventHandler' [Expected element at argument 2, got nil]

[13:49:10] WARNING: editor_test\server.lua:32: Bad argument @ 'getElementType' [Expected element at argument 1, got boolean]

Link to comment
  • Moderators

Wow Wow guys slow down a bit and just let's get back into the first problem:

The code WhoAmI and iAbo told you are just bad ! They will work for at most 1 player at a time !

If a second player start the job, then ab and am that were holding the blip and the marker of the first player will now contain the blip and the marker of the second. So when the first player will exit the vehicle, his blip and marker will still be visible for him because the code destroyed the blip and the marker of the second player.

Is it still what you wanted ? I don't think so. :)

This code will work on client side (you just need to replace the 2 server events by the clients ones).

Or do it nicely doing like this:

function OnEnter ( thePlayer, seat, jacked ) 
    if ( getElementModel ( source ) == 448 ) then 
        outputChatBox ( "Job started !",thePlayer,255,255,0 ) 
        local am = createMarker(-2075.11133,900.70227,64.13281 - 1, "cylinder", 3, 0, 102, 51, 153) 
        local ab = createBlip (-2075.11133,900.70227,64.13281, 0, 3, 102,51,153 ) 
        --Link the blip to the marker this way, if you destroy the marker, it will destroy the blip too 
        setElementParent(ab, am)  
        -- saving the marker on the player element 
        setElementData(source, "jobDestination", am) 
    end 
end 
addEventHandler ( "onVehicleEnter", getRootElement(), OnEnter ) 
  
function OnExit ( thePlayer, seat, jacked ) 
    if ( getElementModel ( source ) == 448 ) then 
        -- getting the marker stored on the player element 
        local am = getElementData(thePlayer, "jobDestination") 
        if am then --if we got the marker 
            destroyElement( source ) 
            outputChatBox ( "You quitted your job.",thePlayer, 255, 0, 0 ) 
            -- destroy the marker and it's children (blip is a child of marker) 
            destroyElement ( am ) 
            setElementData(thePlayer, "jobDestination", false) 
        end 
    end 
end 
addEventHandler ( "onVehicleExit", getRootElement(), OnExit ) 

Link to comment
  • Moderators
function MarkerHit4 ( element ) 
    if (getElementType(element) == "player") then 
        outputChatBox ( "[You win 1034$ !", element, 255, 255, 0 ) 
        givePlayerMoney ( element, 1034 ) 
    elseif (getElementType(element) == "vehicle") then 
        local player = getVehicleOccupant(element) 
        if (player) then 
            outputChatBox ( "[You win 1034$ !", player, 255, 255, 0 ) 
            givePlayerMoney ( player, 1034 ) 
        end 
    end 
end 
addEventHandler( "onMarkerHit", am, MarkerHit4 )  

This addEventHandler is executed at load time (when the server starts) but of course, am is equal to nil because no one can start the job before the server has been started.

And WTF ! Your code would give the player 1034$ twice ! Because the event will be triggered for the vehicle AND the player.

By the way, we can't do it like that with the code I posted above. We have to add the event handler when the marker is created (and optionally use an anonymous function). Here is the new OnEnter function (it's a good practice to start the function names with a lowercase letter => onEnter):

function OnEnter ( thePlayer, seat, jacked ) 
    if ( getElementModel ( source ) == 448 ) then 
        outputChatBox ( "Job started !",thePlayer,255,255,0 ) 
        local am = createMarker(-2075.11133,900.70227,64.13281 - 1, "cylinder", 3, 0, 102, 51, 153) 
        local ab = createBlip (-2075.11133,900.70227,64.13281, 0, 3, 102,51,153 ) 
        --Link the blip to the marker this way, if you destroy the marker, it will destroy the blip too 
        setElementParent(ab, am) 
        -- saving the marker on the player element 
        setElementData(source, "jobDestination", am) 
  
        --Adding the event handler: 
        addEventHandler( "onMarkerHit", am, function ( elem ) 
            if not getElementType(elem) ~= "player" then return end --abort if not a player 
            --abort if the player who entered in it wasn't HIS marker 
            if getElementData(elem, "jobDestination") ~= source then return end 
            local money = 1034 
            givePlayerMoney(elem, money) --give him his cash 
            outputChatBox("[You won "..money.."$ !", elem, 255, 255, 0 ) 
        end) 
  
        -- You probably want to do that too (I didn't think about it in my previous post) 
        -- hide the marker for everyone 
        setElementVisibleTo(marker, root, false) 
        --show it back for the player 
        setElementVisibleTo(marker, thePlayer, true) 
    end 
end 
addEventHandler ( "onVehicleEnter", getRootElement(), OnEnter ) 

As you can see, I also added this:

        -- You probably want to do that too (I didn't think about it in my previous post) 
        -- hide the marker for everyone 
        setElementVisibleTo(marker, root, false) 
        --show it back for the player 
        setElementVisibleTo(marker, thePlayer, true) 

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