Sign in to follow this  
Miliviu

[QUESTION] What's wrong with this? (setTimer)

Recommended Posts

addEventHandler ( "onPlayerDamage", getRootElement (),  
        function ( attacker, weapon ) 
            if ( weapon == 41 ) then 
                if ( getElementType ( source ) == "player" ) then 
                    fadeCamera(source, false) 
                    setTimer(function()
                      fadeCamera(source, true)
                    end, time, 1)
                    outputChatBox ( "Has cegado a " source, attacker, 0, 255, 120 ) 
                    outputChatBox ( "Has sido cegado con un gas pimienta.", source, 0, 255, 120 ) 
                end 
            end 
        end 
    ) 

Hi! I made a pepper spray that blinds when you get hitted by it, but when i used it i have this errors

spray/spray.Lua:7 bad argument @ fadeCamera expected element at argument 1, got nil

spray/spray.Lua:9 attempt to concatenate global source a userdata value

Edited by Miliviu

Share this post


Link to post

It doesn't "know" what source is, you have to pass the argument to the function within your timer

setTimer(function(source) -- Added source here
                      fadeCamera(source, true) -- So now it will get "source" from the function's argument (source)
                    end, time, 1, source) -- And here too, this tells the function what "source" will be

This should resolve that issue...

outputChatBox ( "Has cegado a " source, attacker, 0, 255, 120 ) -- There is no comma between the text string and source

outputChatBox ( "Has cegado a ", source, attacker, 0, 255, 120 ) -- Im still not sure if this would work exactly, but could just do another line

outputChatBox ( "Has cegado a ", source, 0, 255, 120 ) -- Send to the source
outputChatBox ( "Has cegado a ", attacker, 0, 255, 120 ) -- Send to the attacker

 

Edited by ReZurrecti0n
  • Like 1

Share this post


Link to post

Thanks! it worked! But now the message spams every second a player gets hitted by the spray, It's possible to limit the number of messages? Thanks in advantage

addEventHandler ( "onPlayerDamage", getRootElement (),  
        function ( attacker, weapon ) 
            if ( weapon == 41 ) then 
                if ( getElementType ( source ) == "player" ) then 
                    fadeCamera(source, false) 
                    setTimer(function(source)
                      fadeCamera(source, true)
                    end, 5000, 1, source)
                    outputChatBox ( "Has cegado a alguien con un gas pimienta " , attacker, 0, 255, 120 ) 
                    outputChatBox ( "Has sido cegado con un gas pimienta por ." , source, 0, 255, 120 ) 
                end 
            end 
        end 
    ) 

 

Share this post


Link to post
Seconds = 3 -- prevent spamming for 3 sec.
Spamming = {}
	
addEventHandler ( "onPlayerDamage", getRootElement (),  
	function ( attacker, weapon ) 
		if ( weapon == 41 ) then 
			if ( getElementType ( source ) == "player" ) then 
				fadeCamera(source, false) 
				setTimer(function(source)
				  fadeCamera(source, true)
				end, 5000, 1, source)
				if not Spamming[attacker] then 
					Spamming[attacker] = setTimer(function (attac) Spamming[attac] = nil end , Seconds * 1000,1,attacker)
					outputChatBox ( "Has cegado a alguien con un gas pimienta " , attacker, 0, 255, 120 ) 
					outputChatBox ( "Has sido cegado con un gas pimienta por ." , source, 0, 255, 120 ) 
				end
			end 
		end 
	end 
) 

 

  • Thanks 1

Share this post


Link to post

Thanks, it's perfect, have a good day

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.