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

How to stop gun using more ammo than it should every time it is unequipped and re-equipped?

I'm currently attempting to make a gun, and it works well outside of one error; whenever it is unequipped and re-equipped, it uses 1 more ammo than it should per shot. For example; initially, it uses 1 ammo per shot; however, after is unequipped and re-equipped, it uses 2 ammo per shot; and after it is unequipped and re-equipped once more, it uses 3 ammo per shot, and so on. I'm currently using a NumberValue to determine the amount of ammo used per shot, and a RemoteEvent and a server-side script to register damage. This is the section of code that governs the amount of ammo subtracted per shot:

			script.Parent.Activated:Connect(function()
				if Ammo.Value>0 and not Reloading then 
					Ammo.Value = Ammo.Value-1 
			sound:play()
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then 
			script.Parent.Damage:FireServer(Mouse.Target.Parent, 20)

If needed, here is the entire client-side script:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local Values = tool:WaitForChild("Values")
local Ammo = Values:WaitForChild("Ammo")
local MaxAmmo = 10
local mouse = player:GetMouse()
local Reloading = false
local UserInputService = game:GetService("UserInputService")
local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
reloadsound = Instance.new("Sound", workspace)
reloadsound.SoundId = "rbxassetid://269172810"
sound = Instance.new("Sound", workspace)
sound.SoundId = "rbxassetid://162043120"
script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Icon = "rbxassetid://3324802476"
	local function Reload()
			Reloading = true
			reloadsound:play()
			Mouse.Icon = "rbxassetid://3324802476"
			wait(2)
			Ammo.Value = MaxAmmo
			Mouse.Icon = "rbxassetid://3324802476"
			Reloading = false
		end
			script.Parent.Activated:Connect(function()
				if Ammo.Value>0 and not Reloading then 
					Ammo.Value = Ammo.Value-1 
			sound:play()
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then 
			script.Parent.Damage:FireServer(Mouse.Target.Parent, 20)

			end
		elseif Reloading == false then 
			Reload()
		end
		print(Ammo)
	end)
	local Input = game:GetService("UserInputService")
	Input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and Reloading == false and Ammo~= MaxAmmo then 
			reloadsound:play()
		Reload()
		end
		
	end)

	
end)

			

Is there any way to make the ammunition subtracted not increase itself every time the firearm is unequipped and re-equipped?

1 answer

✓ Accepted answer

I think your did not include all the scripts. but the answer to the question should be more or less straight forward.

somehow you equipped the gun, but never totally unequipped it, so the script below,


            script.Parent.Activated:Connect(function() 
                   if Ammo.Value>0 and not Reloading then 
                       Ammo.Value = Ammo.Value-1 
               sound:play() 
               if Mouse.Target.Parent:FindFirstChild("Humanoid") then 
               script.Parent.Damage:FireServer(Mouse.Target.Parent, 20) 
               end 
           elseif Reloading == false then 
               Reload() 
           end 
           print(Ammo) 
       end) 

connected to the same function 3 times after you equip it 3 times. so it does -1 multiple times.

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