Realistic reloading for a weapon/SWEP

Hi there; I was curious if there was a way to code/edit a weapon/SWEP in Gmod to NOT carryover bullets when reloading when the current mag is not totally empty.

For example, if I had a Handgun that had a total of 14 rounds available (Including what was in the current mag) and it had a 7 round cap per mag, and I fired 4 of them, it could reload the 7 rounds taking the whole other available mag instead of just replacing the four fired bullets leaving 10 bullets left.

Any and all help would be appreciated, and thanks in advance!

Set the clip to 0 before calling DefaultReload.

Thanks for the quick response. I’m REALLY new at this, and I’ve just been looking up examples all day to do a code for a weapon (A handgun like I was describing) and I’m editing a pre-existing script just as a test to see if I could get it working. I’m not 100% sure where to change the set clip, and I set the ammo to remove 7 every time it’s reloaded beforehand, but all it does is remove an extra 7 rounds along with whatever number needs replacing.

Obviously I’m doing something wrong, but I can’t seem to find anything online that answers this specific question.

Do self:SetClip1( 0 ) just before self:DefaultReload.

Don’t use self.Weapon, use just self

I tried it, same thing happened; when I set the remove ammo to 0 it just took away what was missing when the clip was reloaded, and with 7 it took away 7 and what was missing as well. If I replace anything besides the self:SetClip1( 0 ) with self instead of self.Weapon it gets lua errors and breaks the gun, although I don’t know if you meant for me to just make sure it was self for the set clip when I wrote it or not. There’s also a strong possibility I didn’t put the self.SetClip1( 0 ) where it was supposed to go, so this is probably on me. (Sorry)

Can you read your own code or not?

Currently, it does this:
If clip1 equals to 0, set it to zero, and do normal reload
If clip1 is not a zero, do normal reload, remove 7 primary ammo and set clip1 to what clip1 already is.

Move self:SetClip1(0) outside of the if, and remove the timer completely.

[editline]5th May 2014[/editline]

Or put another self:SetClip1(0) after “else” so the animations work as intended.

[editline]5th May 2014[/editline]

Or put self:SetClip1(0) after all the ifs, that works too.

Like I said, it’s not my code. It’s from a workshop add on that I just wanted to modify a little (For my own personal needs) to see if I could make the way ammo works more realistic. I know pretty much nothing about coding.

But I figured it out, so thank you for your help. I do appreciate it.

Edit:

Yeah, I’m pretty dumb when it comes to this. Thanks for that vote.