Problem with timers

Hello, I have recently made my own swep with the intention of it being picked up by users as they play and being used at will to give them a quick health buff. I dont want this health buff to be permament and I want it to disapear after a short while allthough I am having trouble getting the timer to work properly.

Here is the code for the timer that I have put in the “innit.lua” file



timer.Create("HPtimer", 5, 1, function() 
			PrintMessage( HUD_PRINTTALK, "You feel the effects of the cola wear off!" );
			self.Owner:SetHealth("100")
			self.Owner:SetMaxHealth("100")
		end)


Here is the code that I have put into the shared.lua file.



function SWEP:PrimaryAttack()
	self.Owner:SetMaxHealth("150")
	self.Owner:SetHealth("150")
	self:Remove() 
	timer.Start("HPtimer")
end


As you can see I am wanting the timer to start when the SWEP is used but this is not the case as the timer starts as soon as the server loads. Here is an error I also get in console allthough this may not be related as im kind of inexperiened…This being my first time coding anything in lua and all =S.

Imgur

Thanks in advance for any help I receive! And for glancing at this thread too! :wink:


function SWEP:PrimaryAttack()
	self.Owner:SetMaxHealth("150")
	self.Owner:SetHealth("150")
	self:Remove() 
	timer.Simple(5, function()
		if ( !IsValid( self ) || !IsValid( self.Owner ) ) then return end
		self.Owner:PrintMessage( HUD_PRINTTALK, "You feel the effects of the cola wear off!" );
		self.Owner:SetHealth("100")
		self.Owner:SetMaxHealth("100")
	end)
end

Why not just create a new player specific timer in place of the timer.Start()?

Because this is unnecessary.

Ah, I see. I completely forgot of timer.Simple :stuck_out_tongue:

Wow, I want to say thanks for the really fast response, but unfortunatly I just tryed that is sp and thankfully no errors pop up in the dev console, but no effects of the timer seem to happen, no text is printed and the health is not set back to 100. I have definatly waited 5 seconds for the effects of the timer to kick in but nothing seems to happen. Could it be something to do with this line?


if ( !IsValid( self ) || !IsValid( self.Owner ) ) then return end

Once again thank you for the quick response Robotboy655, sorry if me kinda coming back like this seems abit rude too :S

  1. Set(Max)Health takes a number not a string
  2. You might want to add “if CLIENT then return end” to the first line of SWEP:PrimaryAttack so it won’t error in multiplayer
  3. Don’t be afraid to edit the code, if you think that “if ( !IsValid( self ) || !IsValid( self.Owner ) ) then return end” might cause problems, remove it and see what happens, your PC won’t explode and your Steam account won’t be banned.

You can always put a bunch of print(“test”)s around the code to see what parts get called and what don’t.



function SWEP:PrimaryAttack()
    if CLIENT then return end

    self.Owner:SetMaxHealth( 150 )
    self.Owner:SetHealth( 150 )
    timer.Simple( 5, function() print("test")
        if ( !IsValid( self ) || !IsValid( self.Owner ) ) then return end print("i am working")
        self.Owner:PrintMessage( HUD_PRINTTALK, "You feel the effects of the cola wear off!" );
        self.Owner:SetHealth( math.min( self.Owner:Health(), 100 ) ) -- It won't reset the health it it is lower then 100, but it will reset if it is higher than 100
        self.Owner:SetMaxHealth( 100 )
    end )
    self:Remove()
end

Check your console for “test” and “i am working”.

Thanks again for the surge of newfound confidence! :wink:
Upon doing what you said I found out that “test” is called in console but after that nothing else was. Upon removing the line of code I found myself face to face with this large error



test

[ERROR] addons/weapon_healcola/lua/weapons/heal_cola/shared.lua:32: Tried to use a NULL entity!
  1. __index - [C]:-1
   2. unknown - addons/weapon_healcola/lua/weapons/heal_cola/shared.lua:32

Timer Failed! [Simple][@addons/weapon_healcola/lua/weapons/heal_cola/shared.lua (line 31)]


As you can probably imagine, this error is quite large and intimidating for someone who has just started out. Im more used to just seeing something like "Expected “(” near “X” "and so on =S

Since I see the number 32 in the error I assume it is refering to line 32 which is the printmessage line. But I can also assume that it will continue on down if that is removed. Again sorry about this! I feel like im eating up all your time! ;_;

If you’re going to change code and it begins to error. Re-paste the code so we can see what is happening.



function SWEP:PrimaryAttack()
    if CLIENT then return end

    self.Owner:SetMaxHealth( 150 )
    self.Owner:SetHealth( 150 )
    timer.Simple( 5, function() print("test")
        self.Owner:PrintMessage( HUD_PRINTTALK, "You feel the effects of the cola wear off!" );
        self.Owner:SetHealth( math.min( self.Owner:Health(), 100 ) ) -- It won't reset the health it it is lower then 100, but it will reset if it is higher than 100
        self.Owner:SetMaxHealth( 100 )
    end )
    self:Remove()
end


As per your request you Handsome devil!

W-w-why did you remove: if ( !IsValid( self ) || !IsValid( self.Owner ) ) then return end

Add it back in.

selt:Remove() is called before self.Owner in the timer is; so self.Owner is NULL. This wouldn’t be a satisfactory way to do it, as it removes the SWEP after five seconds… but it would fix your error :confused:



local shouldExecute = true
function SWEP:PrimaryAttack()
    if CLIENT then return end
    if !shouldExecute then return end
    shouldExecute = false

    self.Owner:SetMaxHealth( 150 )
    self.Owner:SetHealth( 150 )
    timer.Simple( 5, function() print("test")
        self.Owner:PrintMessage( HUD_PRINTTALK, "You feel the effects of the cola wear off!" );
        self.Owner:SetHealth( math.min( self.Owner:Health(), 100 ) ) -- It won't reset the health it it is lower then 100, but it will reset if it is higher than 100
        self.Owner:SetMaxHealth( 100 )
        self:Remove()
    end)
end


Oh, that too ^.

I actually see why it was failing, self was nil cause you remove the weapon.

Fixed:


function SWEP:PrimaryAttack()
    if CLIENT then return end
    local ply = self.Owner

    ply:SetMaxHealth( 150 )
    ply:SetHealth( 150 )
    timer.Simple( 5, function() print("test")
        if ( !IsValid( ply ) ) then return end print("i am working")
        ply:PrintMessage( HUD_PRINTTALK, "You feel the effects of the cola wear off!" );
        ply:SetHealth( math.min( ply:Health(), 100 ) ) -- It won't reset the health it it is lower then 100, but it will reset if it is higher than 100
        ply:SetMaxHealth( 100 )
    end )
    self:Remove()
end

I want to thank you so much for helping me out! No doubt I will be refrencing this in the future if I ever want fading perks again!