Jump to content

Incorrect Element


xDrul

Recommended Posts

Hola,

I've been busy recently trying to script a police job including a jailbreak. The triggerEvent is wrong, and i've got no idea what's wrong in it. If anyone could help me out, that'd be much appreciated.

function polices ( attacker, attackerweapon, bodypart, loss ) 
 if attacker and getElementType(attacker) == "player" then 
        theTeam = getPlayerTeam ( attacker ) 
        theWL = getPlayerWantedLevel ( source ) 
        money = theWL * 2.5 
        if (attackerweapon == 3 or attackerweapon == 23) and (loss > 2 ) and (theWL > 0) then 
            if  getTeamName( theTeam ) == "Police" or getTeamName( theTeam ) == "United States Army" or getTeamName( theTeam ) == "Sheriff Academy" then 
                setElementPosition (source, 4076.3999023438, -1788.5, 3.511967, true) 
                theName = getPlayerName ( source ) 
                theCop = getPlayerName ( attacker )   
                outputChatBox( "*INFO* "..theName.." has been jailed and arrested by "..theCop.."", source, 255, 255, 0 ) 
                local playeraccount = getPlayerAccount ( attacker ) 
                givePlayerMoney (attacker, money) 
                setElementData(source, "isJailed", "true") 
                  setTimer ( newEvent, 1000, 1 ) 
                end 
            end 
        end 
    end 
addEventHandler ("onPlayerDamage", getRootElement(), polices) 
  
function newEvent() 
theName = getPlayerName(source) 
if getElementData(source, "isJailed") == "true" then 
setElementData(source, "isJailed", "false") 
setPlayerWantedLevel(source, 0) 
end 
end 
addEvent("releasePlayer", true) 
addEventHandler("releasePlayer", getRootElement(), newEvent) 

So bassicly, i want to trigger this newEvent after a sec being in jail (1 sec just to test it out, will be changed after tho) but then debugscript tells me: Bad argument, expected element at argument 1, got nill (@ newEvent).

All help 'preciated,

cheers.

Link to comment

With that you're calling the function but not passing the player argument, try:

function polices ( attacker, attackerweapon, bodypart, loss ) 
 if attacker and getElementType(attacker) == "player" then 
        theTeam = getPlayerTeam ( attacker ) 
        theWL = getPlayerWantedLevel ( source ) 
        money = theWL * 2.5 
        if (attackerweapon == 3 or attackerweapon == 23) and (loss > 2 ) and (theWL > 0) then 
            if  getTeamName( theTeam ) == "Police" or getTeamName( theTeam ) == "United States Army" or getTeamName( theTeam ) == "Sheriff Academy" then 
                setElementPosition (source, 4076.3999023438, -1788.5, 3.511967, true) 
                theName = getPlayerName ( source ) 
                theCop = getPlayerName ( attacker )   
                outputChatBox( "*INFO* "..theName.." has been jailed and arrested by "..theCop.."", source, 255, 255, 0 ) 
                local playeraccount = getPlayerAccount ( attacker ) 
                givePlayerMoney (attacker, money) 
                setElementData(source, "isJailed", "true") 
                  setTimer ( newEvent, 1000, 1, source) 
                end 
            end 
        end 
    end 
addEventHandler ("onPlayerDamage", getRootElement(), polices) 
  
function newEvent(player) 
local source = source or player 
if (not source or not isElement(source)) then outputDebugString("Source is false or is not an element") return end 
theName = getPlayerName(source) 
if getElementData(source, "isJailed") == "true" then 
setElementData(source, "isJailed", "false") 
setPlayerWantedLevel(source, 0) 
end 
end 
addEvent("releasePlayer", true) 
addEventHandler("releasePlayer", getRootElement(), newEvent) 

Link to comment
  • Moderators

@Smart: He created the event realeasePlayer and probably want to trigger it properly, so let's use that event ?

function polices ( attacker, attackerweapon, bodypart, loss ) 
    if attacker and getElementType(attacker) == "player" then 
        local theTeam = getPlayerTeam ( attacker ) 
        local theWL = getPlayerWantedLevel ( source ) 
        local money = theWL * 2.5 
        if (attackerweapon == 3 or attackerweapon == 23) and (loss > 2 ) and (theWL > 0) then 
            if  getTeamName( theTeam ) == "Police" or getTeamName( theTeam ) == "United States Army" or getTeamName( theTeam ) == "Sheriff Academy" then 
                setElementPosition (source, 4076.3999023438, -1788.5, 3.511967, true) 
                local theName = getPlayerName ( source ) 
                local theCop = getPlayerName ( attacker )   
                outputChatBox( "*INFO* "..theName.." has been jailed and arrested by "..theCop.."", source, 255, 255, 0 ) 
                local playeraccount = getPlayerAccount ( attacker ) 
                givePlayerMoney (attacker, money) 
                setElementData(source, "isJailed", "true") 
                setTimer(triggerEvent, 1000, 1, "releasePlayer", source) 
            end 
        end 
    end 
end 
addEventHandler ("onPlayerDamage", getRootElement(), polices) 
  
function newEvent() 
    local theName = getPlayerName(source) 
    if getElementData(source, "isJailed") == "true" then 
        setElementData(source, "isJailed", "false") 
        setPlayerWantedLevel(source, 0) 
    end 
end 
addEvent("releasePlayer", true) 
addEventHandler("releasePlayer", getRootElement(), newEvent) 

What did I change ?

- First, I changed all your globals variables into locals variables. Only use global variables if you want that variable to be accessible everywhere on the client-side (if this is a client sided script) or on the server-side (if this is a server sided script).

- Second, I just changed your setTimer to be like this:

setTimer(triggerEvent, 1000, 1, "releasePlayer", source) 

I'm calling the triggerEvent function after 1 second and only 1 time with the arguments: "releasePlayer" as 1st argument and source (which is the guy in jail) as 2nd argument (the source of the "releasePlayer" event)

Regards,

Citizen

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