Periodic Damage?

I wasn’t sure where exactly to post it or if it warranted it’s own thread so yeah just tell me to delete if this is inappropriate. I’m new to both facepunch and lua and I hope I’m not doing any wrong here.
Anyway, I’m trying to create a simple lua file for \autorun\server that damages all players every X seconds (currently set to 3 for testing purposes) to simulate hazardous environments or hunger without actually using DarkRP.
Now, I have no experience actually coding prior to this. I’ve managed to somehow piece together some code (probably with lots of useless parts):

[lua]timer.Create(‘PeriodicDamage’,3,0,function()
for k,v in pairs(player.GetHumans()) do
v:TakeDamage(1, nil)
end
end)[/lua]

This actually seems to work (though I can’t test it out in multiplayer to see if it really affects all players) but it makes the view-shake effect upon damage, which would be highly distracting to gameplay and generally unfitting. Is there any way to negate this, or will I have to find a more complex workaround, such as setting all players’ health to their health minus one and then checking to see if their health is 0, in which case it kills them? (Apparently health can go to 0 without the player actually dying if it’s not caused by actual damage.)
I tried to find something in the garrysmod wiki about that sort of thing, but to no avail.

Again, I apologize if this is a noob question and doesn’t belong here. I would try to learn more, but this seems like a simple thing to do, from my limited understanding, so I hoped someone could give me a little tip. :huh:

Maybe this will stop the screen shaking:



for k,v in pairs(player.GetHumans()) do
    v:SetHealth( v:Health() - 1 )
end


I didn’t even know that TakeDamage made the screen shake…
Also, yeah, this is the right thread for stuff like this.
You could test it on multiplayer if you did a little method from the WAYWO thread:

Alright, thousand thanks!
But yeah, as I suspected, this keeps the player alive even when they’re at 0 health. It’s a better alternative, but I guess I need to put in some code that checks if their health is at 0 and then kills them. Some sort of if/then/else thing, I imagine, or a check. Anyway, if you knew how to do that, it would be appreciated, but I understand if you don’t want to spoonfeed me, hehe.

Sorry, I shouldn’t be spoonfeeding you but most of this stuff doesn’t really need an explanation - to kill the player at 0 health you could just do



if v:Health() =< 0 then v:Kill() end


Below the SetHealth bit

Although of course if he makes it do more than 1 damge that might not work in some situations. In which case



if v:Health() =< 0 then v:Kill() end --Checking if it's 0 or less than 0 instead of just 0


would work better.

That worked perfectly!
You have my sincerest gratitude. :smile:

Thanks, you’re really kind! Also, I think you’re the only person on this entire thread that had a first post with actual working code, so well done!