Help with reloading math

Hello! I’m working on a gun script, and I’m adding reloading. However I seem to be very subpar at math. Even with the computer doing it all for me, I’m sure this is relatively easy and I’m just dumb.

The player is able to reload twice if they use r and click while they have no ammo to reload. The players total ammo can go negative, which causes the players actual ammo to also go negative.

I’ve looked it up, however I didn’t see anything probably because it’s some simple math or something.

Here is my reloading function

local function Reload()
	local reloadanim = hum:LoadAnimation(animr)
	reload.Playing = true
	local spentammo = maxammo - ammo.Value
	local ghostclip
	totalammo.Value -= spentammo
	ghostclip = ammo.Value + spentammo
	if totalammo.Value <= 17 then
		canReload = false
		reloadanim:Play()
		wait(2.647)
		ammo.Value = totalammo.Value
		reloadanim:Stop()
		canReload = true
		return
	end
	if ghostclip <= 0 then
		return
	end
	canReload = false
	reloadanim:Play()
	wait(2.647)
	ammo.Value += spentammo
	reloadanim:Stop()
	canReload = true
	return
end

Thanks in advance.

you’ve shown a lot of unkown variables in your post like ghost clip but anyway code should look something like:

local function Reload()
	local AmmoInGun = 1--the amount of ammo loaded into the gun 
	local AmmoSupplys = 1--ammo reserves 
	local ClipSize = 30 --how much ammo the gun can fit per reload
	AmmoSupplys += AmmoInGun
	AmmoInGun = 0
	if AmmoSupplys <= 0 then
		return
	elseif AmmoSupplys <= ClipSize then
		--run the animation do your wait and set can reload to false
		AmmoInGun += AmmoSupplys
		AmmoSupplys = 0
	else
		--run the animation do your wait and set can reload to false
		AmmoSupplys -= ClipSize
		AmmoInGun += ClipSize
	end
end
2 Likes

Thanks I’ll try this in a minute, ghost clip exists as a pitiful attempt at me solving the issue lol.

Edit:

Thanks your method worked, I knew I was overthinking it appreciate your help!

1 Like