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?
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
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 ).
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.
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
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.
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”…
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.