Constantly depleting/dropping health overtime.

Hey guys, I’ve Googled this multiple times and I can’t seem to find the answer I’m looking for, so I’m asking here. I was wondering how I would go about make it where after a player joins a server, their health starts dropping at a steady rate constantly. For example, the player starts out with 100 HP, and he looses 1 HP every minute or so. When it drops to zero he dies.

I’m REALLY new at coding so I don’t know too much, but this would be for a simple gamemode test derived from the sandbox gamemode, so I’m figuring I’d put it in the Init.lua file, right?

Thanks in advance!

Here’s a simple Bleed System: https://dl.dropboxusercontent.com/u/26074909/tutoring/takedamageinfo_example.lua.html

When using TakeDamage or TakeDamageInfo you don’t need to “kill” them, its done automatically.

Thank you very much, but this is not exactly what I’m looking for. I’m looking for as soon as the player joins the server he starts loosing health without even taking any damage - I’m planing on using it as a sort of hunger system, using food-based items to get his health back up.

I’m guessing there’s probably a way to edit the code to do that, but like I said I have no idea where to begin.

So if all players lose health, its essentially the same code; just in a global timer.

timer.Create( name, delay, repetitions, func )

So, the arguments could be

[lua]timer.Create( “LoseHealthOverTime”, 1, 0, function( )
for k, v in pairs( player.GetAll( ) ) do
// almost identical to bleeding with TakeDamageInfo
end
end );[/lua]

That would make all players lose health every second. Change the 1 to whatever seconds, keep the 0 for it to run forever, and the commented out portion would essentially use TakeDamageInfo( ) on the player ( v ).

I don’t think you understand Acecool, he want a hunger system and was using health as an example.
Edit: Acecool does understand, I am the one who does not.

I don’t know the best route of doing this, but this should work, and is better then using think hook



hook.Add( "PlayerInitialSpawn", "Give_Hunger_Stats", function( ply )
	ply.Hunger = 100;
	StartHunger( ply );
end)

function StartHunger( ply )
	timer.Create( "UpdateHunger"..ply:SteamID(), 60, 0, function() ply.Hunger = ply.Hunger - 1; end )
end


Also, a question, do I need a unique name for each timer? Or will it mess up if two names are the same? can I do what I did by adding the steam id to the end work?

Ohh; something like this - this is a basic hunger system with all of the server code done aside from networking hunger to the player that’ll see it: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_hunger_system.lua.html

?

The thing is, the code I provided could’ve done the same thing with just a variable changed. The basic-hunger system provided will do hunger, and take health. The issue with creating multiple timers is you’ll have to manage them as players join and disconnect. If PlayerDisconnect isn’t called ( which in some cases it doesn’t ), then you’re out of luck unless you use game-events or just use one timer which goes through all players.

Thank you both, but I’ve tried both the bleeding code and the hunger code Adecool provided (I haven’t tried yours Anon because that’s not what I’m really looking for), but they do not work. I put them in the Init.lua file for the gamemode I’m testing, and although I get no lua errors when loading it up, nothing seems to have changed.

And Anon, Adecool was actually correct in what I wanted - the default HL2 health bar you get when you play sandbox slowly loses heath over time, like a few HP, and if you let it hit zero then you die. That’s all I want but these codes don’t seem to be working - the hunger code didn’t even draw a GUI even though in the code it said it would.

I’m sorry, like I said, I have no idea what I’m doing for the most part besides knowing possibly where certain things need to be placed; I figured this would be a rather easy feature to implement but I’m probably just doing something wrong.

Oh, apologies then, I thought you wanted more of a hunger system side then the health side, my mistake.

Also, his name is Acecool, not Adecool :v:

I actually have a friend named Adecool which is why I screwed it up, sorry lol.

So the bleeding system he provided did not really work, although like I said I got no errors on start up.

This is how I went about putting it in the sandbox Init. lua file, is this correct?

(Note: I posted it exactly the way he sent/typed it out in the link he posted, it just got cut off in the screen capture.)

The draw text line is commented out, that’s why it isn’t drawing anything. It only sets up the font, etc for drawing but doesn’t draw it because hunger value isn’t defined which would cause a concatenation nil error.

The bleed code won’t work straight off the bat without you setting it on the player. The hunger version on the other hand will; wait 100 seconds and the hunger will be at 100+ and then you’ll start getting hurt.

So for the Bleed, you’d need to do this on server / init.lua

[lua]hook.Add( “PlayerSpawn”, “PlayerBleedOnSpawn”, function( _p )
– _p:Bleed( _bleedDelay, _bloodLoss, _totalLoss, _inflictor, _attacker )
_p:Bleed( 0, 1, 100, _p, _p )
end );[/lua]

Also,

[lua]if ( _bloodLoss >= _totalLoss || !self:Alive( ) ) then[/lua]

needs to be changed to

[lua]if ( _bloodLost >= _totalLoss || !self:Alive( ) ) then[/lua]

for it to stop correctly if their loss isn’t 100. It’ll stop correctly if they die.

Fixed here: https://dl.dropboxusercontent.com/u/26074909/tutoring/takedamageinfo_example.lua.html

For the hunger/health system: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_hunger_system.lua.html

For it to work, simply put it in init.lua without the client site stuff, that goes into cl_init.lua, and you can remove the entire if SERVER then else end wrapping it. So the server is left with timer and first hook while client is left with hud hook, note it still won’t draw anything because the draw part is commented out because I didn’t network anything.

Alright, awesome; it’s working! :slight_smile:

One last question though; the default hl2 bar, where it says ‘Health’; is there anyway I can change it to say something else, like Energy or what not? Like literally everything else about the HUD is the same, just change the word ‘Health’ to something else. Everything else is working great.

Yeah; you’d need to hide it and create your own; see the HUDPaint portion of the Hunger thing I posted; uncomment the text but instead of using " … hunger" without the quotes, use “… LocalPlayer( ):Health( )” without the quotes; also add a if ( !IsValid( LocalPlayer ) ) then return; end to the line after the hook.Add( “HUDPaint”…

Read this so you know how to properly disable hud elements - don’t use table.HasValue, use the direct-access method.
https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua.html

it’s “CHudHealth”

Awesome, thanks a bunch!

Hey, would it be possible to have access to all your tutoring files?

I’m still moving things around to better organize. Some of the examples are essentially fully working “module / plugin” style of work with a ton of commenting to allow others to learn from them; but since they’re drop-ins I’ll probably put them in the systems folder, or something.

It’ll probably not be “finalized” while its all on drop-box, so I may bring my site back up, create a database and a system to highlight the code for when I make the full Lua class available for those wanting to learn; among the class will be tons of code examples, a base gamemode with a lot of the functionality, helper-functions, etc.

So, yes, I plan on making all of them available in an easy to navigate location with the coding standards file ( plus other coding styles; as the class will encompass not only mine, but many other peoples standards because in the real-world programmers are expected to adhere to a projects coding standard or a company-wide standard. The more standards shown, I believe it will help users understand all of the different ways things can be written and still work on top of showing how the EBNF/BNF ties into syntax ) linked from each tutorial and assignment.

The class will be open-book, all of the content will be available at all times; learn at your own pace whether it means covering all material in a week, or a month, or however long.