Attempt to index local 'ply' (a nil value)

I’m trying to add in a concommand that will return a players ammo for the weapon they are holding, back into their inventory.


function GetCurWepTotalAmmo( ply )
 
	actwep = ply:GetActiveWeapon()
	if ( ! actwep ) then return -1 end
 
	return ply:GetAmmoCount(actwep:GetPrimaryAmmoType())
end

concommand.Add( "giveammoback", function() GetAmmoForCurrentWeapon()
end)


Clearly I aint no genius at this, as I am still learning how to do a few things here and there. I used the code from Here and just used is as it was, but I got the same error i’m having the issue with but instead it was pointing to “LocalPlayer” So I removed that, and changed MySelf to ply (thinking it would work… Obviously not) If you know why its not working, or have a few pointers that could help me out, that’d be great. Anything helps.





concommand.Add( "giveammoback", function( ply )

    local actwep = ply:GetActiveWeapon()
    return !actwep and -1 or ply:GetAmmoCount(actwep:GetPrimaryAmmoType())

end )


More efficient. Your error was that you weren’t passing the ply arg.



concommand.Add( "giveammoback", function( -> ply <-) GetAmmoForCurrentWeapon( -> ply <- )
end)


Also, you’ll want to make your functions and variables local unless otherwise necessary (rarely).

Another way would simply be



concommand.Add( "giveammoback", GetAmmoForCurrentWeapon)