Regenerate ammo for a specific weapon while it is holstered?


I am creating my very first SWEP (and making many many mistakes) and I seem to be ignorant to the means for a function.

The weapon is based off the Welder Tool from the Killing Floor series in that it recharges its ammunition over time.

I have implemented this as such:

The issue here is that this obviously only works while the weapon is out and I want this to apply even if the weapon is holstered.

If the answer is so painfully obvious that I risk a concussion from facepalming, let me know.

Thank you for your time.

Hey, so the reason this isn’t working is because SWEP:Think is only called when you have the weapon in your hand.

Unfortunately I can’t remember what hooks are available but you could always use the GM:Tick function that has access to a list of players with the SWEP (Which players are added to with the SWEP.Equip hook and removed with SWEP:OnDrop) and loop through that.

Also if possible I would avoid using timers, you could try using CurTime() to store when it was last updated when it goes past your threshold you can add ammo then.

Sorry this reply is a bit of a mess but if you want anything cleared up just let me know.

Good luck :grinning:

(Small update: make sure you have your corner cases covered, I think if the player dies it should trigger onDrop but you want to make sure otherwise you’ll be getting spammed with errors)

1 Like

Thank you for getting back to me so soon, and thanks for the advice.

I’m going to take a looksie at all these and come back after.(Hopefully with a working SWEP!)

1 Like

This is what I figured out so far.

I have gone a different direction with this.

My friend had the idea to save and compare the CurTIme() when the weapon has been holstered and deployed once again and multiply the difference by the amount of ammo to regenerate.

This goes back to using my original SWEP:Think function, but I have updated it too!

Ah good to hear you got it working :slightly_smiling_face:

Proper smart move my man :+1:

I would advise you to localize stuff like the ammo amount you receive outside of the function or on the SWEP (SWEP.chargeAmount) and using that instead of “hard-coding” it in. This makes it easier for other people to read/find and for you as well.