Entity:TakeDamage not causing entity to take damage?

I have the following code:


function SWEP:Initialize()

	local ply = self.Owner
	local wep = self.Weapon

	hook.Add("Tick","MySWEP.Tick",function()
		local On = ply:GetNWBool("DamageOn",false)
		if On then
			if ply:GetNWInt("Counter",0)>=10 then
				print("Player should have took damage.")
				ply:SetNWInt("Counter",0)
				if SERVER then
					ply:TakeDamage(1,ply,wep)
				end
			else
				ply:SetNWInt("Counter",ply:GetNWInt("Counter",0)+1)
			end
		end
	end)

Through prints, I have discovered that the counter does work. However, the damage does not work. Why not?

[editline]30th April 2016[/editline]

I realize it has to do with ply and wep, but I don’t understand why those don’t work. Is there a workaround?

  1. Don’t use a Tick hook
  2. You have to use Deploy to get the owner.

What do you suggest I use, then? I need it to activate continuously until the player is dead, and SWEP:Think doesn’t work because it only works when the weapon is out.

Can you give a description of what you’re trying to do?

Every 10th tick, the player takes damage if a network boolean is true.

That’s oddly specific; do you want it to happen when the player has the weapon out? When they are just carrying it?

Maybe something like:



local ply
local damageOn = true
function SWEP:DoDamage()
    ply:TakeDamage(1,ply,self)
    if(damageOn) then
        timer.Simple(10,self:DoDamage())
    end
end

function SWEP:Initialize()
    ply = self:GetOwner()
    timer.Simple(10,self:DoDamage())
end


--Make the bool false with some other function if ya want?


10 ticks, not seconds.

[editline]30th April 2016[/editline]

Also, the owner isn’t available in Initialize.

Wasnt sure about the owner part, I guess self.Owner is fine.

Using ticks can be useful for things, but im not sure if this application is appropriate. Ticks are not fixed to any interval, lag and such could affect its interval…Why dont you just use a very small timer delay? A delay of 1, or 0.1??

Don’t quote me on this but, I remember reading somewhere a tick happens about 6 times a second normally.

[editline]30th April 2016[/editline]

If you really really really want to do this, although it doesn’t make sense outside of velocity and collision stuff.


local count = 0
hook.Add("Tick","whatever",function()
    count = count + 1
    if(count%10== 0) then   --gets the remainder of count divided by 10, will be a value of 0-9 repeating
        --called every 10th tick.
    end
end)

--You would at some point want to reset count to 0 so the number doesnt get too long-should be fine for a while?