Tool debounce issue

I am making one gun but when I Unequip it my tools broke. Like I can still still reload and aim with it without being Equipped.

GIF:

--------Checks if Mouse is Clicked--------
tool.Equipped:Connect(function(Mouse)
	if not debounce then
		plr.PlayerGui.AmmoGui.Enabled = true
		plr.PlayerGui.AmmoGui.AmmoText.Text = "Ammo: ".. tostring(ammo.Value) .. "/" .. tool.MaxAmmo.Value

		Mouse.Button1Down:Connect(function()
			if tool.Ammo.Value > 0 then
				debounce = true
				RemoteEvent:FireServer(tool.BulletLocation.Position, tool.BulletLocation.Orientation, Mouse.Hit.p)
				Gun_Sound:Play()
				Shoot:Play()
				tool.Ammo.Value = tool.Ammo.Value - 1
			else
				Empty_Sound:Play()
				Shoot:Play()
			end
		end)
		mouse.Button2Down:Connect(function()

			local camera = game.Workspace.CurrentCamera

			ScopeAim:Play()
			camera.FieldOfView = 10

		end)

		mouse.Button2Up:Connect(function()

			local camera = game.Workspace.CurrentCamera

			camera.FieldOfView = 70
			ScopeAim:Stop()
		end)
	end		
end)	

--------Unequip gun--------

tool.Unequipped:Connect(function()
	plr.PlayerGui.AmmoGui.Enabled = false
	debounce = false
end)

What error is in the output???

1 Like

Try stopping the anim when the tool is unequip on the function
like:

tool.Unequipped:Connect(function()
plr.PlayerGui.AmmoGui.Enabled = false
debounce = false
reloadAnim:Stop()
end)

The listeners remain active even after you have unequipped your tool. Try like this:

local button1DownListener = nil
local button2DownListener = nil
local button2UpListener = nil

--------Checks if Mouse is Clicked--------
tool.Equipped:Connect(function(Mouse)
	if not debounce then
		plr.PlayerGui.AmmoGui.Enabled = true
		plr.PlayerGui.AmmoGui.AmmoText.Text = "Ammo: ".. tostring(ammo.Value) .. "/" .. tool.MaxAmmo.Value

		button1DownListener = Mouse.Button1Down:Connect(function()
			if tool.Ammo.Value > 0 then
				debounce = true
				RemoteEvent:FireServer(tool.BulletLocation.Position, tool.BulletLocation.Orientation, Mouse.Hit.p)
				Gun_Sound:Play()
				Shoot:Play()
				tool.Ammo.Value = tool.Ammo.Value - 1
			else
				Empty_Sound:Play()
				Shoot:Play()
			end
		end)
		button2DownListener = mouse.Button2Down:Connect(function()

			local camera = game.Workspace.CurrentCamera

			ScopeAim:Play()
			camera.FieldOfView = 10

		end)

		button2UpListener = mouse.Button2Up:Connect(function()

			local camera = game.Workspace.CurrentCamera

			camera.FieldOfView = 70
			ScopeAim:Stop()
		end)
	end		
end)	

--------Unequip gun--------

tool.Unequipped:Connect(function()
	plr.PlayerGui.AmmoGui.Enabled = false
	debounce = false
        if button1DownListener then
            button1DownListener:Disconnect()
            button1DownListener = nil
        end
        if button2DownListener then
            button2DownListener:Disconnect()
            button2DownListener = nil
        end
        if button2UpListener then
            button2UpListener:Disconnect()
            button2UpListener = nil
        end
end)
2 Likes

I dont have any erros in output

Sorry I posted it wrong.

Your script works but I still can reload the gun. I didnt show all script but I can show just for understand.

script:
--------Varibles--------
local plr = game:GetService(“Players”).LocalPlayer

local tool = script.Parent

local anim
local Shoot
local reload
local ScopeAim

local Gun_Sound = script.Parent:WaitForChild(“SniperShoot”)
local Empty_Sound = script.Parent:WaitForChild(“EmptyGunSound”)
local Reload_Sound = script.Parent:WaitForChild(“ReloadGunSound”)

local clipSize = tool:WaitForChild(“Ammo”).Value
local ammo = tool:WaitForChild(“Ammo”)

local button1DownListener = nil
local button2DownListener = nil
local button2UpListener = nil
local debounce = false

--------Animations--------
tool.Equipped:Connect(function()
local char = plr.Character or plr.CharacterAdded:Wait()

game.ReplicatedStorage.ConnectM6D:FireServer(tool.BodyAttach)

char.Torso.ToolGrip.Part0 = char.Torso
char.Torso.ToolGrip.Part1 = tool.BodyAttach

local Humanoid = char:FindFirstChild('Humanoid')  
anim = Humanoid:LoadAnimation(script:WaitForChild("Idle"))
Shoot = Humanoid:LoadAnimation(script:WaitForChild("Shoot"))
reload = Humanoid:LoadAnimation(script:WaitForChild("Reload"))
ScopeAim = Humanoid:LoadAnimation(script:WaitForChild("Aim"))
anim:Play()

end)

tool.Unequipped:Connect(function()

game.ReplicatedStorage.DisconnectM6D:FireServer()

if not anim then return end  	
anim:Stop()
reload:Stop()
ScopeAim:Stop()

end)

--------UserInputService Setup--------
local userInput = game:GetService(“UserInputService”)

--------Mouse Icon--------
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = “rbxassetid://117431027”

--------Remote Event Setup--------
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local RemoteEvent = ReplicatedStorage:WaitForChild(“ShootEvent”)

--------Checks if Mouse is Clicked--------
tool.Equipped:Connect(function(Mouse)
if not debounce then
plr.PlayerGui.AmmoGui.Enabled = true
plr.PlayerGui.AmmoGui.AmmoText.Text = "Ammo: "… tostring(ammo.Value) … “/” … tool.MaxAmmo.Value

	button1DownListener = Mouse.Button1Down:Connect(function()
		if tool.Ammo.Value > 0 then
			debounce = true
			RemoteEvent:FireServer(tool.BulletLocation.Position, tool.BulletLocation.Orientation, Mouse.Hit.p)
			Gun_Sound:Play()
			Shoot:Play()
			tool.Ammo.Value = tool.Ammo.Value - 1
		else
			Empty_Sound:Play()
			Shoot:Play()
		end
	end)
	button2DownListener = mouse.Button2Down:Connect(function()

		local camera = game.Workspace.CurrentCamera

		ScopeAim:Play()
		camera.FieldOfView = 10

	end)

	button2UpListener = mouse.Button2Up:Connect(function()

		local camera = game.Workspace.CurrentCamera

		camera.FieldOfView = 70
		ScopeAim:Stop()
	end)
end		

end)

--------Unequip gun--------

tool.Unequipped:Connect(function()
plr.PlayerGui.AmmoGui.Enabled = false
debounce = false
if button1DownListener then
button1DownListener:Disconnect()
button1DownListener = nil
end
if button2DownListener then
button2DownListener:Disconnect()
button2DownListener = nil
end
if button2UpListener then
button2UpListener:Disconnect()
button2UpListener = nil
end
end)

--------Checks if the letter R is pressed for reload-------- (Here is the reload function)
userInput.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then
local keycode = input.KeyCode
if keycode == Enum.KeyCode.R then
reload:Play()
Reload_Sound:Play()
Reload_Sound.Ended:Wait()
tool.Ammo.Value = 7
end
end
end
end)

--------Update ammo GUI--------

ammo.Changed:Connect(function()
plr.PlayerGui.AmmoGui.AmmoText.Text = "Ammo: "… tostring(ammo.Value) … “/” … tool.MaxAmmo.Value
end)

You should do something similar for the user input. Same way you did for mouse buttons. Connect when tool is equipped and disconnect when tool unequipped.

Ok I will make that… But thank you soo much.

1 Like

Wait I am still having one issue on the reload.
It doesnt reload when I equippe again.

the script:

--------Varibles--------
local plr = game:GetService("Players").LocalPlayer

local tool = script.Parent

local anim
local Shoot 
local reload 
local ScopeAim

local Gun_Sound = script.Parent:WaitForChild("SniperShoot")
local Empty_Sound = script.Parent:WaitForChild("EmptyGunSound")
local Reload_Sound = script.Parent:WaitForChild("ReloadGunSound")

local clipSize = tool:WaitForChild("Ammo").Value
local ammo = tool:WaitForChild("Ammo")

local debounce = false


--------Animations--------
tool.Equipped:Connect(function()
	local char = plr.Character or plr.CharacterAdded:Wait()

	game.ReplicatedStorage.ConnectM6D:FireServer(tool.BodyAttach)

	char.Torso.ToolGrip.Part0 = char.Torso
	char.Torso.ToolGrip.Part1 = tool.BodyAttach

	local Humanoid = char:FindFirstChild('Humanoid')  
	anim = Humanoid:LoadAnimation(script:WaitForChild("Idle"))
	Shoot = Humanoid:LoadAnimation(script:WaitForChild("Shoot"))
	reload = Humanoid:LoadAnimation(script:WaitForChild("Reload"))
	ScopeAim = Humanoid:LoadAnimation(script:WaitForChild("Aim"))
	anim:Play()
end)

tool.Unequipped:Connect(function()

	game.ReplicatedStorage.DisconnectM6D:FireServer()
	
	if not anim then return end  	
	anim:Stop()
	reload:Stop()
	ScopeAim:Stop()
end)

--------UserInputService Setup--------
local userInput = game:GetService("UserInputService")

--------Mouse Icon--------
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = "rbxassetid://117431027"

--------Remote Event Setup--------
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("ShootEvent")

--------Checks if Mouse is Clicked--------
tool.Equipped:Connect(function(Mouse)
	if not debounce then
		plr.PlayerGui.AmmoGui.Enabled = true
		plr.PlayerGui.AmmoGui.AmmoText.Text = "Ammo: ".. tostring(ammo.Value) .. "/" .. tool.MaxAmmo.Value

		button1DownListener = Mouse.Button1Down:Connect(function()
			if tool.Ammo.Value > 0 then
				debounce = true
				RemoteEvent:FireServer(tool.BulletLocation.Position, tool.BulletLocation.Orientation, Mouse.Hit.p)
				Gun_Sound:Play()
				Shoot:Play()
				tool.Ammo.Value = tool.Ammo.Value - 1
			else
				Empty_Sound:Play()
				Shoot:Play()
			end
		end)
		button2DownListener = mouse.Button2Down:Connect(function()

			local camera = game.Workspace.CurrentCamera

			ScopeAim:Play()
			camera.FieldOfView = 10
		end)
		

		button2UpListener = mouse.Button2Up:Connect(function()
			
			local camera = game.Workspace.CurrentCamera

			camera.FieldOfView = 70
				ScopeAim:Stop()
		end)
	end		
end)	

--------Checks if the letter R is pressed for reload--------
RkeyBindLister = userInput.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R then
				debounce = true
					reload:Play()
					Reload_Sound:Play()
					Reload_Sound.Ended:Wait()
					tool.Ammo.Value = 7
			end
		end
	end
end)

--------Update ammo GUI--------

ammo.Changed:Connect(function()
	plr.PlayerGui.AmmoGui.AmmoText.Text = "Ammo: ".. tostring(ammo.Value) .. "/" .. tool.MaxAmmo.Value
end)

--------Unequip gun--------

tool.Unequipped:Connect(function()
	plr.PlayerGui.AmmoGui.Enabled = false
	debounce = false
	if button1DownListener then
		button1DownListener:Disconnect()
		button1DownListener = nil
	end
	if button2DownListener then
		button2DownListener:Disconnect()
		button2DownListener = nil
	end
	if button2UpListener then
		button2UpListener:Disconnect()
		button2UpListener = nil
	end
	if RkeyBindLister then
		RkeyBindLister:Disconnect()
		RkeyBindLister = nil
	end
end)

Put the userInput listener in toolEquiped

--------Checks if the letter R is pressed for reload--------
RkeyBindLister = userInput.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R then
				debounce = true
					reload:Play()
					Reload_Sound:Play()
					Reload_Sound.Ended:Wait()
					tool.Ammo.Value = 7
			end
		end
	end
end)

^ Put this in toolEquiped function.

Don’t forget to declare RkeyBindLister like the other listeners.

1 Like

Ok I get it I was puting outside the equied function thank you soo much. :slight_smile:

1 Like