Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Gun Reload bug?

I have made a gun that works perfectly fine except the reload part. The problem is that if i have less then 12 of my spareammo and less then 12 ammo in the clip, then if i reload i will get 0 ammo and 0 in spareammo or even less sometimes i got -6 spareammo?

function reload(key)
	key:lower()
	if key == "r" and spareammo >= 1 and not reloading and Ammo <= 11 then
		game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = true
		print("Reloading")
		reloading = true
		IsFiring = false
		wait(Reloadtime)
		reloading = false
		--Ammo is set to the value 12 and spareammo is 30 from the start
		spareammo = spareammo + (Ammo - 12)		
		if spareammo >= 12 then
			Ammo = 12
		elseif spareammo <= 11 and spareammo > 0 then
			Ammo = spareammo
		elseif spareammo == 0 then
			Ammo = 0
		end
		game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = false
		
	end
end


mouse.KeyDown:connect(reload)
Gun.Deactivated:connect(stopshooting)
Gun.Activated:connect(shoot)

if you dont understand the problem then i can try to explain it better for you.

1 answer

✓ Accepted answer

Your problem is the your calculating how much spare ammo there is by subtracting 12 off of your ammo, even if you don't have 12 spare ammo to subtract. Your check can be done a bit better so I cleaned it up for you. Instead of checking when it is between certain intervals, it just keep adding ammo until the magazine is filled or there is no more spare ammo.

function reload(key)
	key:lower()
	if key == "r" and spareammo >= 1 and not reloading and Ammo <= 11 then
		game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = true
		print("Reloading")
		reloading = true
		IsFiring = false
		wait(Reloadtime)
		reloading = false

		for i = 1, 12 do -- This is the for loop that determines the ammo
			if ammo < 12 and spareammo > 0 then
				ammo = ammo + 1
				spareammo = spareammo - 1
			end
		end
		
		game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = false
	end
end


mouse.KeyDown:connect(reload)
Gun.Deactivated:connect(stopshooting)
Gun.Activated:connect(shoot)

1 comment

0

Thanks it works

This discussion is preserved as it was. New answers, comments, and votes are closed.