Jump to content

onClientPlayerWeaponFire


NeverGiveup

Recommended Posts

the element is an object. I found this https://wiki.multitheftauto.com/wiki/On ... jectDamage, and I got an error says source is nil? eventho the element was created.

function outputLoss(loss) 
    if not isElement(source) then return end 
    if getElementModel(source) ~= 897 then return end 
    local oldHealth = getElementHealth(source) 
    local hitX, hitY, hitZ = getElementPosition(source) 
    setTimer(function() 
        local newHealth = getElementHealth(source) 
        outputChatBox("Real loss: "..(newHealth-oldHealth)) 
        outputChatBox("Theoretical loss: "..loss) 
        if getElementHealth(source) == 0 then 
            destroyElement(source) 
            createExplosion(hitX, hitY, hitZ, 8, true, -1.0, false) 
        end 
    end,100,1) 
end 
addEventHandler("onClientObjectDamage", root, outputLoss) 

Link to comment

You're using source inside the timer function, source is only defined inside the handler function.

Just define a player variable that points to the source. Or you can send the source as an argument to the timer function.

function outputLoss(loss) 
    if not isElement(source) then return end 
    if getElementModel(source) ~= 897 then return end 
    local oldHealth = getElementHealth(source) 
    local hitX, hitY, hitZ = getElementPosition(source) 
    local object = source 
    setTimer(function() 
        local newHealth = getElementHealth(object) 
        outputChatBox("Real loss: "..(newHealth-oldHealth)) 
        outputChatBox("Theoretical loss: "..loss) 
        if getElementHealth(object) == 0 then 
            destroyElement(object) 
            createExplosion(hitX, hitY, hitZ, 8, true, -1.0, false) 
        end 
    end,100,1) 
end 
addEventHandler("onClientObjectDamage", root, outputLoss) 

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