Jump to content

destroyElement... bad argument when tying to destroy marker


Franc[e]sco

Recommended Posts

I got a problem... i'm adding the final touches to my gamemode... and i added a spawnkilling protection... to make it look nicer i attach a cylinder marker to the player until the spawnkilling protection goes down (5 secs)... and destroy element says bad argument when it should destroy the marker... here's some snippets from the code:

If the spawn protection is setted to true in the map file and a player spawns, create the marker and set the timer to destroy the marker.

    if (getElementData(getElementByIndex("spawnprotect", 0), "activated") == "true") then 
        setTimer(spawnProtectEnd, 5000, 1, source) 
        setElementData(source, "spawnProtected", true) 
        spawnProtectMarker = createMarker(0, 0, 0, "cylinder", 2, 255, 0, 0, 150) 
        attachElementToElement(spawnProtectMarker, source) 
    end 

If the player is spawn protected recover his loss when someone shoots him and don't allow headshots on him

function onPlayerDamaged (attacker, attackerweapon, bodypart, loss) 
    if(getElementData ( source, "spawnprotected") == true) then 
        setElementHealth(source, getElementHealth(source)+loss) 
    elseif (getElementData(getElementByIndex("headshots", 0), "activated") == "true") then 
        if(bodypart == 9) then 
            killPlayer(source, attacker, attackerweapon, bodypart) 
        end 
    end  
end 
addEventHandler("onPlayerDamage", getRootElement (), onPlayerDamaged) 

Destroy the marker and remove spawn protection

function spawnProtectEnd (player) 
    setElementData ( player, "spawnprotected", false ) 
    destroyElement(spawnProtectMarker) 
end 

Link to comment

You shouldn't need to detatch it. The only reason it would give bad argument is if it isn't really a marker (i.e. you're passing the wrong variable, you've modified the variable by accident, you've got a function with the same name as the variable, you've already deleted the marker)

Link to comment

I can't find any errors... here's the whole code:

-- Simple DM - coded by Franc[e]sco 
--//--------------------------------------------------------------------------// 
local playerid = thePlayer 
local rSpawnPoints = {} 
killingSpreeDisplay = textCreateDisplay() 
killingSpreeActive = false 
killingSpreeMsg = textCreateTextItem("", 0.7, 0.05) 
textDisplayAddText(killingSpreeDisplay, killingSpreeMsg) 
--//--------------------------------------------------------------------------// 
function onPlayerSpawns ( ) 
    playSoundFrontEnd(source, #rSpawnPoints) 
    local rand = randInt(1, #rSpawnPoints) 
    setElementPosition(source, rSpawnPoints[rand].x, rSpawnPoints[rand].y, rSpawnPoints[rand].z + 1) 
    setPlayerRotation(source, rSpawnPoints[rand].f) 
    setPlayerSkin(source, rSpawnPoints[rand].skin) 
    giveWeapon(source, rSpawnPoints[rand].fist, 1) 
    giveWeapon(source, rSpawnPoints[rand].melee, 1) 
    giveWeapon(source, rSpawnPoints[rand].handgun, 200) 
    giveWeapon(source, rSpawnPoints[rand].shotgun, 20) 
    giveWeapon(source, rSpawnPoints[rand].smgun, 5000) 
    giveWeapon(source, rSpawnPoints[rand].mgun, 5000) 
    giveWeapon(source, rSpawnPoints[rand].rifle, 15) 
    giveWeapon(source, rSpawnPoints[rand].heavy, 5) 
    giveWeapon(source, rSpawnPoints[rand].projectile, 5) 
    giveWeapon(source, rSpawnPoints[rand].special1, 30) 
    giveWeapon(source, rSpawnPoints[rand].gift, 1) 
    giveWeapon(source, rSpawnPoints[rand].special2, 1) 
    giveWeapon(source, rSpawnPoints[rand].detonator, 1) 
    if (getElementData(getElementByIndex("spawnprotect", 0), "activated") == "true") then 
        setTimer(spawnProtectEnd, 5000, 1, source) 
        setElementData(source, "spawnProtected", true) 
        spawnProtectMarker = createMarker(0, 0, 0, "cylinder", 2, 255, 0, 0, 150) 
        attachElementToElement(spawnProtectMarker, source) 
    end 
end 
addEventHandler ( "onPlayerSpawn", getRootElement(), onPlayerSpawns ) 
--//--------------------------------------------------------------------------// 
function spawnThePlayer (playerid) 
    spawnPlayer(playerid, 0.0,0.0,5.0, 0, 0) 
    fadeCamera(playerid, true) 
    toggleAllControls( playerid, true ) 
end 
--//--------------------------------------------------------------------------// 
function killThePlayer (playerid) 
    killPlayer ( playerid, nil, 255, 4 )     
end 
addCommandHandler("kill",killThePlayer) 
--//--------------------------------------------------------------------------// 
function onGmodeLoad (startedResource) 
    print( "\nLOADED: Simple Deathmath by Franc[e]sco\n" ) 
    spawns = getElementsByType ("spawnpoint") 
    for k, v in pairs(spawns) do 
        local x = getElementData(v,"x") 
        local y = getElementData(v,"y") 
        local z = getElementData(v,"z") 
        local f = getElementData(v,"f") 
        local fist = getElementData(v,"fist") 
        local melee = getElementData(v,"melee") 
        local handgun = getElementData(v,"handgun") 
        local shotgun = getElementData(v,"shotgun") 
        local smgun = getElementData(v,"smgun") 
        local mgun = getElementData(v,"mgun") 
        local rifle = getElementData(v,"rifle") 
        local heavy = getElementData(v,"heavy") 
        local projectile = getElementData(v,"projectile") 
        local special1 = getElementData(v,"special1") 
        local gift = getElementData(v,"gift") 
        local special2 = getElementData(v,"special2") 
        local detonator = getElementData(v,"detonator") 
        local skin = getElementData(v, "skin") 
        table.insert(rSpawnPoints, { x=x, y=y, z=z, f=f, fist=fist, melee=melee, handgun=handgun, shotgun=shotgun, smgun=smgun, mgun=mgun, rifle=rifle, heavy=heavy, projectile=projectile, special1=special1, gift=gift, special2=special2, detonator=detonator, skin=skin }) 
    end 
    setTimer(spawnEveryOne, 1500, 1) 
    call(getResourceFromName("scoreboard"), "resetScoreboardColumns") 
    call(getResourceFromName("scoreboard"), "addScoreboardColumn", "score") 
    call(getResourceFromName("scoreboard"), "addScoreboardColumn", "killingspree") 
    if (getElementData(getElementByIndex("killmessages", 0), "activated") == "true") then 
        if (getResourceState(getResourceFromName("killmessages")) ~= "running") then 
            startResource(getResourceFromName("killmessages")) 
        end 
    end 
end   
addEventHandler ( "onResourceStart", getRootElement (), onGmodeLoad ) 
--//--------------------------------------------------------------------------// 
function onGmodeEnd (theResourceStopped) 
    if (getResourceState(getResourceFromName("killmessages")) == "running") then 
        stopResource(getResourceFromName("killmessages")) 
    end 
end 
addEventHandler ( "onResourceStop", getRootElement (), onGmodeEnd ) 
--//--------------------------------------------------------------------------// 
function spawnEveryOne () 
    local playersToSpawn = getElementsByType ("player") 
    for k,v in ipairs ( playersToSpawn ) do 
        setTimer(spawnThePlayer, 1500, 1, v) 
        setElementData ( v, "score", 0 ) 
        setElementData ( v, "killingspree", 0 ) 
    end 
end 
--//--------------------------------------------------------------------------// 
function destroyGameText (display) 
    local everyPlayer = getElementsByType ("player") 
    for k,v in ipairs (everyPlayer) do 
        textDisplayRemoveObserver(display, v) 
    end 
    killingSpreeActive = false 
end 
--//--------------------------------------------------------------------------// 
function onPlayerConnect () 
    local playerName = getClientName(source) 
    sendClientMsg("Welcome, " .. playerName .. ", this server is actually running 'Simple Deathmatch by Franc[e]sco', enjoy your stay.", source, "green") 
    setTimer(spawnThePlayer, 1500, 1, source) 
    setElementData ( source, "score", 0 ) 
    setElementData ( source, "killingspree", 0 ) 
    if(killingSpreeActive == true) then 
        textDisplayAddObserver(killingSpreeDisplay, source) 
    end 
end 
addEventHandler("onPlayerJoin", getRootElement (), onPlayerConnect) 
--//--------------------------------------------------------------------------// 
function OnPlayerExit( reason ) 
    setElementData( source, "score", 0 ) 
    setElementData ( source, "killingspree", 0 ) 
end 
addEventHandler( "onPlayerQuit", getRootElement (), OnPlayerExit ) 
--//--------------------------------------------------------------------------// 
function onPlayerDeath (ammo, attacker, weapon, bodypart) 
    setTimer(spawnThePlayer, 10000, 1, source) 
    sendClientMsg("You died! 10 seconds till respawn", source, "red") 
    if attacker then 
        setElementData( attacker, "score", getElementData(attacker, "score")+1 ) 
        setElementData( attacker, "killingspree", getElementData(attacker, "killingspree")+1 ) 
        if (bodypart == 9) then 
            sendClientMsgToAll("* " .. getClientName(attacker) .. " just headshotted " .. getClientName(source) .. " !", "green") 
        elseif (bodypart == 4) then 
            sendClientMsgToAll("* " .. getClientName(attacker) .. " just ass shotted " .. getClientName(source) .. " ! Ow, that hurts :(", "green") 
        end 
        if (getElementData(attacker, "killingspree") == 5) then 
            textItemSetText(killingSpreeMsg, getClientName(attacker) .. " is dominating!") 
            textItemSetColor(killingSpreeMsg, 255, 255, 255, 180) 
            killingSpreeActive = true 
            setTimer(destroyGameText, 5000, 1, killingSpreeDisplay) 
        elseif (getElementData(attacker, "killingspree") == 10) then 
            textItemSetText(killingSpreeMsg, getClientName(attacker) .. " is on a killing spree!") 
            textItemSetColor(killingSpreeMsg, 255, 255, 0, 180) 
            setTimer(destroyGameText, 5000, 1, killingSpreeDisplay) 
            sendClientMsg("* You get 5 granades for your 10 kills", attacker) 
            giveWeapon(attacker, 16, 5) 
            killingSpreeActive = true 
        elseif (getElementData(attacker, "killingspree") == 15) then 
            textItemSetText(killingSpreeMsg, getClientName(attacker) .. " is unstoppable!") 
            textItemSetColor(killingSpreeMsg, 255, 128, 64, 180) 
            sendClientMsg("* You get 5 more granades for your 15 kills", attacker) 
            giveWeapon(attacker, 16, 5) 
            killingSpreeActive = true 
            setTimer(destroyGameText, 5000, 1, killingSpreeDisplay) 
        elseif (getElementData(attacker, "killingspree") == 20) then 
            textItemSetText(killingSpreeMsg, getClientName(attacker) .. " owned the entire server!") 
            textItemSetColor(killingSpreeMsg, 200, 0, 0, 180) 
            setTimer(destroyGameText, 5000, 1, killingSpreeDisplay) 
            sendClientMsg("* You get 10 more granades for your 20 kills", attacker) 
            giveWeapon(attacker, 16, 10) 
            killingSpreeActive = true 
        end 
        if (getElementData(source, "killingspree") > 5 and attacker ~= source) then 
            sendClientMsgToAll("* " .. getClientName(attacker) .. " stopped " .. getClientName(source) .. " from doing " .. getElementData(source, "killingspree")+1 .. " straight kills!", "grey") 
        end 
        if(killingSpreeActive == true) then 
            textItemSetScale(killingSpreeMsg, 3.0) 
            local everyPlayer = getElementsByType ("player") 
            for k,v in ipairs (everyPlayer) do 
                textDisplayAddObserver(killingSpreeDisplay, v) 
            end 
        end 
    end 
    setElementData( source, "killingspree", 0 ) 
end 
addEventHandler("onPlayerWasted", getRootElement (), onPlayerDeath)           
--//--------------------------------------------------------------------------// 
function onPlayerDamaged (attacker, attackerweapon, bodypart, loss) 
    if(getElementData ( source, "spawnprotected") == true) then 
        setElementHealth(source, getElementHealth(source)+loss) 
    elseif (getElementData(getElementByIndex("headshots", 0), "activated") == "true") then 
        if(bodypart == 9) then 
            killPlayer(source, attacker, attackerweapon, bodypart) 
        end 
    end  
end 
addEventHandler("onPlayerDamage", getRootElement (), onPlayerDamaged) 
--//--------------------------------------------------------------------------// 
function spawnProtectEnd (player) 
    setElementData ( player, "spawnprotected", false ) 
    detachElementFromElement(spawnProtectMarker, player) 
    destroyElement(spawnProtectMarker) 
end 
--//--------------------------------------------------------------------------// 
function sendClientMsg (message, player, color) 
    local r,g,b = 0, 0, 0 
    if (color == "red") then 
        r = 206 
        g = 0 
        b = 5 
    elseif (color== "green") then 
        r = 0 
        g = 187 
        b = 10 
    elseif (color == "grey") then 
        r = 144 
        g = 144 
        b = 144 
    end 
    outputChatBox(message, player, r, g, b) 
end 
--//--------------------------------------------------------------------------// 
function sendClientMsgToAll (message, color) 
    local r,g,b = 0, 0, 0 
    if (color == "red") then 
        r = 206 
        g = 0 
        b = 5 
    elseif (color== "green") then 
        r = 0 
        g = 187 
        b = 10 
    elseif (color == "grey") then 
        r = 144 
        g = 144 
        b = 144 
    end 
    outputChatBox(message, getRootElement (), r, g, b) 
end 
--//--------------------------------------------------------------------------// 
  
  
  

Link to comment

Hmm, how about this function?

function spawnProtectEnd (player) 
     setElementData ( player, "spawnprotected", false ) 
     local elements = getAttachedElements(player) -- get all elements attached to the player as a table (shouldn't be any others, but hey, you never know.. : ) ) 
     for k,v in ipairs(elements) do -- search the table... 
          if(getElementType(v) == "marker") then -- ... and when a marker-element is found 
               -- detachElementFromElement(v, player) -- try with and without this line 
               destroyElement(v) -- delete it. 
          end 
     end 
 end 

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