Jump to content

setElementHealth


Reezmi

Recommended Posts

Why this only affects me, not the zombies?

function Magic1(source) 
    local radius = 10 
    local zombies = getElementsByType ( "ped" ) 
    local x, y, z = getElementPosition(source) 
    local attackSphere = createColSphere(x, y, z, radius) 
    nearbyZombies = getElementsWithinColShape(attackSphere, zombies) 
    destroyElement(attackSphere) 
    for index, nearbyZombies in ipairs( nearbyZombies ) do 
    setElementHealth ( nearbyZombies, tonumber(getElementHealth(nearbyZombies) or 0) - 25 ) 
    end 
    outputDebugString('attack') 
end 

Link to comment

First advice, don't name variables with the same names (eg. nearbyZombies). Also, try to avoid naming parameters with predefined variable names like source, root, etc. since the function called from an event will also pass the hidden source variable.

Before you set element's health, check if the element is not "you".

Link to comment

I tried checking if peds are not player with isElement but it doesnt work. But if I remove if isElement(ped) it does take health from peds but it also takes health from me. Its like peds and me are the same.

I just dont get it.

Here's current code:

  
function Magic1(source) 
    local radius = 10 
    local x, y, z = getElementPosition(source) 
    local attackSphere = createColSphere(x, y, z, radius) 
    nearbyZombies = getElementsWithinColShape(attackSphere, source) 
    destroyElement(attackSphere) 
    for index, nearbyZombies in ipairs( nearbyZombies ) do 
    if isElement(ped) then 
    setElementHealth ( nearbyZombies, tonumber(getElementHealth(nearbyZombies) or 0) - 25 ) 
    else 
    outputDebugString('yes') 
    end 
    end 
    outputDebugString('attack') 
end 

Link to comment
function Magic1(source) 
     local r = 10 
     local x, y, z = getElementPosition(source) 
     local sp = createColSphere(x, y, z, r) 
     local peds = getElementsWithinColShape(sp, "ped") 
     destroyElement(sp) 
     for _, p in ipairs (peds) do 
          if (getElementType(p) ~= "player") then 
               setElementHealth(p, (getElementHealth(p) or 0) - 25) 
          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...