Sounds playing whenever the mouse is clicked?

  1. What do you want to achieve? I already have a simple gun system, but whenever I put the gun away and after the debounce goes back to false and I click, it makes the shooting noise and fires the event again despite the tool not even being out.
local mouse = game.Players.LocalPlayer:GetMouse()
local Debounce = false
local UserInputService = game:GetService("UserInputService")

while wait() do
	script.Parent.Activated:Connect(function()
		mouse.Button1Down:Connect(function()
			if Debounce == false then
				Debounce = true
				script.Parent.Sound:Play()
				script.Parent.Fire:FireServer(mouse.Hit.p)
			end
		end)
	end)
	script.Parent.Unequipped:Connect(function()
		Debounce = true
		local descendants = workspace:GetDescendants()
		for i = 1, #descendants do
			if descendants[i]:IsA("Sound") then
				descendants[i]:Stop()
			end
		end
	end)
	if Debounce == true then
		wait(15)
		Debounce = false
	end
end

There are no errors again, I tried adding the for i descendants loop to stop all the sounds, that didn’t work, I also tried adding this all in a while wait() loop so that I could from there check and assign the debounces. That didn’t work either.

1 Like
  1. Don’t put all of this inside a loop because there’s no point to it.
  2. .Acticated already handles the mouse click. Because you used a Button1Down connection it will be listening for a mouse click all the time and unlike the .Activated which won’t fire when if the tool is unequipped.
  3. Put the debounce inside the .Activated connection.
local Debounce = false	

script.Parent.Activated:Connect(function()
	if Debounce == false then
		Debounce = true
		script.Parent.Sound:Play()
     	script.Parent.Fire:FireServer(mouse.Hit.p)
        wait(15)
        Debounce = false
    end
end)

script.Parent.Unequipped:Connect(function()
	Debounce = true
	local descendants = workspace:GetDescendants()
	for i = 1, #descendants do
		if descendants[i]:IsA("Sound") then
			descendants[i]:Stop()
		end
	end
end)
2 Likes

Try that, no need for InputService

script.Parent.InputEnded:Connect(function(input)
if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then 
-- code
       end
end)

Would not apply here, already stated I added the loop after I noticed it would play the sound without the tool even equipped, I need the mouse to get the mouse’s position.

Does the script not work when you add the mouse variable?

Because

I added the mouse variable before it stopped working… and again, how else would I send the mouse in the event without the mouse