Infinite Ammo Code for SP

Hey everybody. I’ve been wanting an infinite ammo code for singleplayer/LAN, but I know basically nothing about lua.

I’ve gotten a couple codes from what other people posted in other threads about it, I put it in a text file and then turned it into lua, and then put it in my GMod “lua” folder, but nothing happened. I have no idea if I’m posting it in the right place, or if I need to do something to the code to make it work, but here’s the code I’ve been using:

local function UnlimitedAmmo()    for k,v in pairs(player.GetAll()) do
    curwep = v:GetActiveWeapon()
        if (v:Alive()) then
            if (curwep:Clip1() < 100) then
            if (curwep:Clip2() < 100) then
            if v:GetAmmoCount(curwep:GetPrimaryAmmoType() < 100)then
            if v:GetAmmoCount(curwep:GetSecondaryAmmoType() < 100)then

I got this code from raccoon2112, where he posted it in a thread about infinite ammo in 2009. I’m not sure if it’s because it’s so old that it’s not working, but I have no clue about any of this. I need some help on what exactly to do, like if anything in the code needs to be edited, if I’m putting it in the wrong place, or if I just need a different code

Also, my apologies if there’s already a thread explaining how to do this, but I wasn’t able to find one

Put it in lua/autorun.

I did that, but it still isn’t working. I’ve tested weapons, and they still run completely out of ammo without regenerating whatsoever

You have to open the file every time you want more ammo; that, or make it a global function, then bind a key to lua_run UnlimitedAmmo().

[editline]1st September 2014[/editline]

If you want it to be “truly infinite,” then you’d have to put it in a Think hook, which is just expensive and inefficient.

You could put it on a timer, which would be the best solution

Yeah but still, Timers can lag after a while, depending on how much code is inside one, but they are inexpensive.

No one seems to have noticed the code posted already has a timer in it to keep it running after it is first called. Not very elegant, but it should be working. The only reason it would stop running is if there was an error.

The issue looks like the parenthesis around the ammo check:
[lua]if v:GetAmmoCount(curwep:GetPrimaryAmmoType() < 100)then[/lua]
should be
[lua]if v:GetAmmoCount(curwep:GetPrimaryAmmoType()) < 100 then[/lua]
and the same for secondary ammo.