Jump to content

Player lose health


Tails

Recommended Posts

Hi, I'm trying to create a script that lets the players health gradually drop by -1, kind of as if they were starving.

I have no idea how to learn this because the wiki doesn't explain me this.

function Starve(thePlayer) 
  
    setTimer(function()  
    SetElementHealth( thePlayer, getElementHealth(thePlayer) - 2 ), 1000, 0) 
  
    end 
end 
addEventHandler("onResourceStart", Starve) 

Link to comment

Ty Solidsnake14.

Do I need to put type="client" in the meta file, server or none?

function Starve(thePlayer) 
    setTimer( 
    function()  
    setElementHealth( thePlayer, getElementHealth(thePlayer) - 2 ) 
    , 1000, 0) 
    end 
end 
addEventHandler("onPlayerSpawn", getRootElement(), Starve) 

Right now it's saying unexpected symbol near "," when debugging.

Can you also explain me a little bit as to why I need to put a 2nd argument in the eventHandler while on other scripts I don't? And what exactly means rootElement and what is the different between root and getRootElement?

Thanks in advance! It is much appreciated!

Link to comment
function Starve ( ) 
    setTimer ( 
        function ( thePlayer ) 
            if ( thePlayer and isElement ( thePlayer ) ) then 
                setElementHealth ( thePlayer, getElementHealth ( thePlayer ) - 2 ) 
            end 
        end 
        ,1000, 0, source 
    ) 
end 
addEventHandler ( "onPlayerSpawn", getRootElement(), Starve ) 

Is a server side script.

Link to comment
To pass the 'thePlayer' argument to the function executed by the timer.

but if the timer has it's own function:

setTimer(function()end,ms,exec) 

then you don't really have to pass the argument through.

example:

function Starve ( ) 
    setTimer(function() 
        if ( source and isElement ( source ) ) then 
            setElementHealth ( source, getElementHealth ( source ) - 2 ) 
        end 
    end,1000, 0) 
end 
addEventHandler ( "onPlayerSpawn", getRootElement(), Starve ) 

Link to comment
To pass the 'thePlayer' argument to the function executed by the timer.

but if the timer has it's own function:

setTimer(function()end,ms,exec) 

then you don't really have to pass the argument through.

example:

function Starve ( ) 
    setTimer(function() 
        if ( source and isElement ( source ) ) then 
            setElementHealth ( source, getElementHealth ( source ) - 2 ) 
        end 
    end,1000, 0) 
end 
addEventHandler ( "onPlayerSpawn", getRootElement(), Starve ) 

You need, it's a separated function.

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