Jump to content

Player lose health


Tails

Recommended Posts

Posted

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) 

Discord: its.tails

Posted

1: You got missing argument 2 at addEventHandler.

2: "onResourceStart" has no player argument.

3: Lua is case-sensetive, so is not SetElementHealth, but setElementHealth.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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!

Discord: its.tails

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

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
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 ) 

My in-game name: Jaysds1

Retired CMG Scripter

World Of Tanks GameMode (Open-Source): https://github.com/Jaysds1/mtasa-wot-gamemode

Online GUI-Editor (WIP): https://forum.mtasa.com/topic/47678-online-gui-editor/

 

sE5Qm.png

TiV3C.png

img.php?id=0&text=Lua%20Scripter

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

"[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007

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