Max ammo not subtracting current ammo

This is the reloading code:

if input.KeyCode == Enum.KeyCode.R and framework.module.ammo < framework.module.NormalAmmo and isSprinting == false and framework.module.maxAmmo > 0 then
	
		canShoot = false
		isReloading = true
		reloadAnim:Play()
		wait(framework.module.reloadTime)
		canShoot = true
		isReloading = false
		framework.module.ammo = framework.module.NormalAmmo
		framework.module.maxAmmo -= framework.module.ammo
		
		
		
	end
end)

Shooting code:

while wait() do
	
	if isShooting  and framework.module.ammo > 0 and isReloading ~= true and canShoot == true then
		fireAnim:Play()
		framework.module.ammo -= 1
		
		
		game.ReplicatedStorage.Events.Shoot:FireServer(framework.viewmodel.Muzzle.Position, mouse.Hit.p, framework.module.damage, framework.module.headshot)
		
		mouse.Button1Up:Connect(function()
			isShooting = false
		end)
		wait(framework.module.fireRate)
	end

ammo variable is referring to a variable in a module script located in ReplicatedStorage. Value = 30

This script is located in StarterPlayerScripts

For some odd reason, if I reload my gun, say I shot 1 bullet, and the mag capacity is 30 bullets, and my max ammo is 120 bullets, I should have had 119 total bullets now, when I reload, since I shot one. But some reason, it subtracts 30 bullets every time I reload.

framework.module.ammo -= 1

I’m guessing is that this code ^ up there is not updating every time I shoot.

Before I look into it more further, you can have a variable which counts bullets being shot in the current Mag, then subtract it to the max ammo and set the bullets being shot variable to 0. Print the Current Ammo Value in the shooting part.

2 Likes

You said a Value, I am assuming a ValueBase instance.

You want to use framework.module.maxAmmo.Value = yourAmountHere

Also, there is a problem with your code. There is a while wait loop that creates a connection every iteration. I am not available atm and will be back to help unless someone helps.

local function shootingLoop()
    local connection
while ifShooting and framework.module.ammo.Value > 0 and isReloading ~= true and canShoot == true do
    fireAnim:Play()
    framework.module.ammo.Value -= 1

    game.ReplicatedStorage.Events.Shoot:FireServer(framework.viewmodel.Muzzle.Position, mouse.Hit.p, framework.module.damage, framework.module.headshot)
		
		connection = mouse.Button1Up:Connect(function()
			isShooting = false
		end)
		task.wait(framework.module.fireRate)
	end

    connection:Disconnect()
end
end


Sorry for the bad formatting, I am on mobile. This might not work, but this is how it should be done. Maybe have canShoot as a catch-all that checks all of those instead of imposing the many conditions.

It’s definitely not ammo. Value other wise there would be an error and the ammo is subtracts the MaxAmmo when it does not say ammo.Value.

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