Gun reloading system doesn't work fine

Chello mate!
I’m currently having problem in mein gun system.
The problem is, reloading the gun while ammo/reserve ammo less than magazine capacity will return magazine value to the max and reserve to negative number.
Here the screenshot :
image

Script regarding the reloading function:

reloadEvent.OnServerEvent:Connect(function(clientThatPressed)
	if not isReloading.Value and (magazine.Value < refferenceMagazine.Value) and gun.Equipped and ammo.Value > 0 then
		local readyReload = clientThatPressed.Character:FindFirstChild("Humanoid"):LoadAnimation(gunReload)
		canFire.Value = false
		isReloading.Value = true
		
		if consolidate.Value == true then
			keepRefference = refferenceMagazine.Value
			readyReload:Play()
			handle.GunReload:Play()
			task.wait(reloadTime.Value)			
			calculation = (keepRefference)
			ammo.Value = (ammo.Value - calculation)
			magazine.Value = refferenceMagazine.Value
			
			isReloading.Value = false
			canFire.Value = true
		else 
			keepRefference = refferenceMagazine.Value
			readyReload:Play()
			handle.GunReload:Play()
			task.wait(reloadTime.Value)
			calculation = (keepRefference - magazine.Value) 
			ammo.Value = (ammo.Value - calculation) -- Here the problem start!
			magazine.Value = refferenceMagazine.Value
			
			isReloading.Value = false
			canFire.Value = true
		end
	end
end)

Is there any fix to this problem?

1 Like

try this

reloadEvent.OnServerEvent:Connect(function(clientThatPressed)
	if not isReloading.Value and (magazine.Value < refferenceMagazine.Value) and gun.Equipped and ammo.Value > 0 then
		local readyReload = clientThatPressed.Character:FindFirstChild("Humanoid"):LoadAnimation(gunReload)
		canFire.Value = false
		isReloading.Value = true
		
		if consolidate.Value == true then
			keepRefference = refferenceMagazine.Value
			readyReload:Play()
			handle.GunReload:Play()
			task.wait(reloadTime.Value)			
			calculation = (keepRefference)
			if ammo.Value >= calculation then -- added this line
				ammo.Value = (ammo.Value - calculation) -- moved this line inside the if statement
			end -- added this line
			magazine.Value = refferenceMagazine.Value
			
			isReloading.Value = false
			canFire.Value = true
		else 
			keepRefference = refferenceMagazine.Value
			readyReload:Play()
			handle.GunReload:Play()
			task.wait(reloadTime.Value)
			calculation = (keepRefference - magazine.Value) 
			if ammo.Value >= calculation then -- added this line
				ammo.Value = (ammo.Value - calculation) -- moved this line inside the if statement
			end -- added this line			
			magazine.Value = refferenceMagazine.Value
			
			isReloading.Value = false
			canFire.Value = true
		end
	end
end)
3 Likes

Thanks for your help, but now the ammo value wont drop down when magazine value is greater.

Try using math.clamp(). I think it works like this:

ammo.Value -= math.clamp(0, ammo.Value, calculation)

I may be using it wrong, so you can look at the documentations for it.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.