Jump to content

onPlayerHealthChange event? (detect health change)


johny46

Recommended Posts

Posted

Hi. I've made a resource, which adds a Health column to the scoreboard. It gets updated on onPlayerSpawn and onPlayerDamage events. It works quite well, but it doesn't react to slapping, simply Slap doesn't call the onPlayerDamage event. Is there any other way to check whether player's health has changed? Of course other than getting the player's health every second and comparing it for changes :P If not, maybe you could consider adding such event to MTA?

Posted

Wouldn't it spam my server a lot? I guess onClientRender is triggered very often.

Solidsnake, I didn't know that we are able to create our own events, thanks! I'll have to look further into it tomorrow.

PS. Cześć :)

  • Moderators
Posted
Wouldn't it spam my server a lot? I guess onClientRender is triggered very often.

and use getTickcount() to reduce data overclow.

getTickCount() 

Posted

Using Table

Try this (but debug first i made it in 2 Minutes without checking for bugs or else)

  
-- Pleas add [TiLex Produkt] as commentary to your script while using this  
  
addEvent("onPlayerHealthChange", true) 
local playerOldHealthTable = {} 
  
function healthChangedTriggerFunction() 
    setTimer(function() 
        local playerTable = getElementsByType("player") 
        for index, player in ipairs(playerTable) do 
        local oldLive = playerOldHealthTable[player] 
            if oldLive and (oldLive > 0) then 
                local curLive = getElementHealth(player) 
                if not (curLive == oldLive) then 
                 
                local changedlive = 0 
                if (curLive > oldLive) then 
                    changedlive = (curLive - oldLive) 
                else 
                    changedlive = (oldLive - curLive) 
                end 
                    triggerEvent ( "onPlayerHealthChange", player, player, curLive, oldLive, changedlive) -- trigger Event 
                    playerOldHealthTable[player] = curLive 
                end 
            else 
                playerOldHealthTable[player] = (getElementHealth(player)) 
            end 
        end 
    end, 500, 0) 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), healthChangedTriggerFunction) 
  
  
  
-- EXAMPEL FUNKTION FOR EVENT 
function healthChanged(player, newHealth, oldHealth, changedLive) 
    -- Live changed. Here some stuff 
outputChatBox("Your Live had changed from " ..tostring(oldHealth).. " to " ..tostring(newHealth)), player, 255, 0, 0) 
end 
addEventHandler("onPlayerHealthChange", getRootElement(), healthChanged) 
  

EventName: onPlayerHealthChange

Parameters: element Player, int newhealth, int oldHealth, int howManyHealthChanged

[sERVERSIDE]

Posted

Thanks for help. I appreciate your solutions, especially Schlammy's one, but it's too much overcomplicated for my simple needs. I followed the Solidsnake14's idea and I just do

setElementData(player,"Health",getElementHealth(player)) 

every time I slap a player. Works without any problem as of now :) Again thanks for your replies :)

  • 10 years later...
Posted
On 22/05/2013 at 23:18, Jacob Lenn said:
onClientRender 
getElementHealth 
setElementData 
 

 

Isnt it better to use this functions?

onClientRender in combination with setElementData is never good.

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