Jump to content

Help with event Handler


xXMADEXx

Recommended Posts

Hey all, i made this simple code, but i am not sure what event handler to put in to make the wanted star create.

function lvl(player) 
    local wanted = getPlayerWantedLevel(player) 
    local x,y,z = getElementPosition(player) 
    if wanted >= 0 then 
        obj = createObject(2147,x,y,z) 
        attachElements(obj,player,0,0,1.2) 
    elseif wanted <= 0 then 
        destroyElement(obj) 
    end 
end 
addEventHandler("?????",lvl) 
  

Link to comment
Well, there's no event for when wanted level changes, so you maybe should make a timer to loop all players, or make a custom event.

I tried this, but error on line 5, and im not sure what it is.

setTimer( 
function (player) 
    local wanted = getPlayerWantedLevel(player) 
    local x,y,z = getElementPosition(player) 
    if (wanted <= 0) then 
        destroyElement(obj) 
    elseif (wanted >= 0) then 
        obj = createObject(1247,x,y,z) 
        attachElements(obj,player,0,0,1.5) 
    end 
end,1000,1) 
  

Edited by Guest
Link to comment
setTimer ( 
    function ( ) 
        for _, player in ipairs ( getElementsByType ( "player" ) ) do 
            local wanted = getPlayerWantedLevel ( player ) 
            local x, y, z = getElementPosition ( player ) 
            if ( wanted <= 0 ) then 
                destroyElement ( obj ) 
            elseif ( wanted >= 0 ) then 
                obj = createObject ( 1247, x, y, z ) 
                attachElements ( obj, player, 0, 0, 1.5 ) 
            end 
        end 
    end 
    ,1000, 0 
) 

This is not efficient though.

Link to comment
setTimer ( 
    function ( ) 
        for _, player in ipairs ( getElementsByType ( "player" ) ) do 
            local wanted = getPlayerWantedLevel ( player ) 
            local x, y, z = getElementPosition ( player ) 
            if ( wanted <= 0 ) then 
                destroyElement ( obj ) 
            elseif ( wanted >= 0 ) then 
                obj = createObject ( 1247, x, y, z ) 
                attachElements ( obj, player, 0, 0, 1.5 ) 
            end 
        end 
    end 
    ,1000, 0 
) 

I had to switch it to this:

    setTimer ( 
        function ( ) 
            for _, player in ipairs ( getElementsByType ( "player" ) ) do 
                local wanted = getPlayerWantedLevel ( player ) 
                local x, y, z = getElementPosition ( player ) 
                if ( wanted >= 0 ) then 
                    obj = createObject ( 1247, x, y, z ) 
                    attachElements ( obj, player, 0, 0, 1.5 ) 
                elseif ( wanted <= 0 ) then 
                    destroyElement(obj) 
                end 
            end 
        end 
        ,1000, 0 
    ) 

but, when the player is no long wanted, the star will not delete :S

This is not efficient though.

Link to comment
stars = { } 
  
setTimer ( 
    function ( ) 
        for _, player in ipairs ( getElementsByType ( "player" ) ) do 
            local wanted = getPlayerWantedLevel ( player ) 
            local x, y, z = getElementPosition ( player ) 
            if ( wanted == 0 ) then 
                if ( isElement ( stars [ player ] ) ) then 
                    destroyElement ( stars [ player ] ) 
                end 
            else 
                stars [ player ] = createObject ( 1247, x, y, z ) 
                attachElements ( stars [ player ], player, 0, 0, 1.5 ) 
            end 
        end 
    end 
    ,1000, 0 
) 

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