Jump to content

Hit timer


marty000123

Recommended Posts

Hi.

function slap()
x,y,z = getElementPosition(source)
setElementPosition(source,x,y,z+5)
    if ??? then
        cancelEvent ()
    end
end
addEvent("slap",true)
addEventHandler("slap",getRootElement(),slap)

This is a part of my script. As you can see it slaps the player with 5 height. But I want to allow this only if the players hasn't been hit for 2 straight minutes. But I have no idea how to make that. I assumed that I had to put something between ''if'' and ''then'', but after a few tries I couldn't figure out what was needed. Help would be appreciated!

Marty

Edited by marty000123
Link to comment

Try this also you can't cancel every event actually just some event's check the wiki page :

 

aTimer = {  }
function slap (     )
    if isTimer ( aTimer[source] ) then return false end
    local x,y,z = getElementPosition ( source )
    setElementPosition ( source,x,y,z+5 )
    aTimer[source] = setTimer ( function( source ) aTimer [source] = nil end,120000,1,source ) 
end
addEvent( "slap",true )
addEventHandler( "slap",getRootElement(),slap )

 

Edited by iPrestege
Link to comment
10 hours ago, iPrestege said:

Try this also you can't cancel every event actually just some event's check the wiki page :

 


aTimer = {  }
function slap (     )
    if isTimer ( aTimer[source] ) then return false end
    local x,y,z = getElementPosition ( source )
    setElementPosition ( source,x,y,z+5 )
    aTimer[source] = setTimer ( function( source ) aTimer [source] = nil end,120000,1,source ) 
end
addEvent( "slap",true )
addEventHandler( "slap",getRootElement(),slap )

 

Hi. Thanks for your help!

Your timer works, but I want the timer to start after being hit, so you can slap yourself anytime you want but if you get any damage (BY A PLAYER) then you shouldn't be able to slap for 2 straight minutes. That's what I meant. Thanks though.

Marty

Link to comment

Hi Pollo, thanks for the tip. But how do I do that? I know that it has to do something with https://wiki.multitheftauto.com/wiki/OnPlayerDamage and https://wiki.multitheftauto.com/wiki/ResetTimer but I don't really know how to put this into the script without :Oing something up again. 

I have to add something like this, I guess?:  (it probably doesnt make any sense, so please help)

function playerDamage_text ( attacker, weapon, bodypart, loss )
    if ( bodypart => 0 ) then
           resetTimer(aTimer)
           outputChatBox ( "You can't be shot less than 2 minutes ago in order to use this function!", getRootElement (), 255, 0, 0 )
    end
end
addEventHandler ( "onPlayerDamage", getRootElement (), playerDamage_text )
	
Edited by marty000123
Link to comment
function playerDamage_text ( attacker, weapon, bodypart, loss )
	--You do not need to know where the player got damaged (i'm talking about bodypart)
	if attacker then --if the player got damage from a player, that parameter is the attacker player. Flase otherwise
		if isTimer( aTimer[source] ) then --you also need to check if the 2 minutes are still ongoing
			--the check was made also to make sure the argument of resetTimer is valid
			resetTimer(aTimer[source]) --aTimer is the table, actually you need the timer
			outputChatBox ( "You can't be shot less than 2 minutes ago in order to use this function!", getRootElement (), 255, 0, 0 ) --actually i don't understand the meaning of the message but it's ok XD
		end
	end
end
addEventHandler ( "onPlayerDamage", root, playerDamage_text ) --root = getRootElement(), take a look to predefined variables

I made some changes to the code, but you are facing the right direction!

Try this and let me know if it's working or if i forgot something ;)

Edited by LoPollo
Link to comment
51 minutes ago, LoPollo said:

function playerDamage_text ( attacker, weapon, bodypart, loss )
	--You do not need to know where the player got damaged (i'm talking about bodypart)
	if attacker then --if the player got damage from a player, that parameter is the attacker player. Flase otherwise
		if isTimer( aTimer[source] ) then --you also need to check if the 2 minutes are still ongoing
			--the check was made also to make sure the argument of resetTimer is valid
			resetTimer(aTimer[source]) --aTimer is the table, actually you need the timer
			outputChatBox ( "You can't be shot less than 2 minutes ago in order to use this function!", getRootElement (), 255, 0, 0 ) --actually i don't understand the meaning of the message but it's ok XD
		end
	end
end
addEventHandler ( "onPlayerDamage", root, playerDamage_text ) --root = getRootElement(), take a look to predefined variables

I made some changes to the code, but you are facing the right direction!

Try this and let me know if it's working or if i forgot something ;)

Hi!

Thanks so much for the detailled script and explaination! But there's a but, there shouldn't be any timer until he gets hit. I mean like, I can slap myself 500 times, but when I get hit by a player, the timer sets itself on 2 minutes etc. You know what I mean? About the outputChatBox, it was wrongly placed :D I meant to put it on the place where the player clicks on ''Slap Me'' but it would refuse to slap him because he would've been hit less than 2 minutes ago. Can you fix that?

Link to comment

About the outputChatBox i understand more or less the meaning, but something like this could be better?

You can't be shot less than 2 minutes ago in order to use this function!

to

You must get no damage for 2 straight minutes to use the slap function

but yeah it would be better to place it when trying to slap BUT the 2m are not passed yet

I think it's for debug and it will be removed once the script is finished so it's not a real problem :)

 

Well... there's the script. I removed the timer from the slap, and... well check it

local aTimer = {  }

function slap ( )
	if isTimer ( aTimer[source] ) then --if the player got damage in the last 2 minutes then do not let he slap
		--outputChatBox( "You got damage in the last 2 minutes, you can't use this!", source)
		return 
	end 
	local x,y,z = getElementPosition ( source )
	setElementPosition ( source,x,y,z+5 )
end
addEvent( "slap",true )
addEventHandler( "slap", root, slap )

function playerDamage_text ( attacker, weapon, bodypart, loss )
	if attacker then
		if isTimer( aTimer[source] ) then --he got damage in the last 2 minutes, reset the timer
			resetTimer(aTimer[source])
		else --he did NOT got damage in the last 2 minutes, so he now has and the timer starts
			aTimer[source] = setTimer ( 
				function( thePlayer ) --"thePlayer is the element passed"
					aTimer[thePlayer] = nil 
				end,
			120000, 1, source ) --source is passed to the timer function as "thePlayer"
		end
	end
end
addEventHandler ( "onPlayerDamage", root, playerDamage_text )

I hope i did not forgot anything and did not make errors, so let us know if this work xD

Remember to set the right source element when triggering the event "slap"

Edited by LoPollo
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...