InputBegan/InputEnded firing twice

I’m currently making a gun system, and i’d like to detect when the player presses mousebutton1

I’m currently using UserInputService, but the problem is that for some reason, InputBegan/InputEnded fires twice, which doubles the player’s damage. I’ve tried using 3 different debounce/cooldown systems, but they just don’t work. Is there any way to fix this issue?

Here is my input code:

local db = false -- ignore this, i was trying to fix it with this but obviously didn't work.

UserInput.InputEnded:Connect(function(input, gpe)
	print("ended") -- this is how i know it's being pressed twice
	if not gpe and input.UserInputType == Enum.UserInputType.MouseButton1 and not db then
		db = true
		local isHolding = script.Parent.HoldServiceHandler.GetIsHoldingFromOutsideScript:Invoke()
		local isSprinting = script.Parent.SprintServiceHandler.GetIsSprintingFromOutsideScript:Invoke()

		ShootInstance:OnInput(player.Character, isHolding, isSprinting)
		wait(0.1)
		db = false
	end
end)

Here are also some screenshots:

image

1 Like

Mmmh, may sounds dumb but have you tried changing the wait(0.1) value to something higher ? The usual causes of bugs in debounce is that the cooldown is way too short

1 Like

If that doesn’t work I’ll just use GetMouse()

local mouse = player:GetMouse() -- You have to initialize player before that or it won't work

local db = false

mouse.Button1Up:Connect(function()
	print("ended") -- this is how i know it's being pressed twice
	if not gpe and input.UserInputType == Enum.UserInputType.MouseButton1 and not db then
		db = true
		local isHolding = script.Parent.HoldServiceHandler.GetIsHoldingFromOutsideScript:Invoke()
		local isSprinting = script.Parent.SprintServiceHandler.GetIsSprintingFromOutsideScript:Invoke()

		ShootInstance:OnInput(player.Character, isHolding, isSprinting)
		wait(0.1) -- Try changing the value idk
		db = false
	end
end)

1 Like

that won’t work, since it’s being fired twice on the same EXACT frame.

That’ll probably work, but I’m still confused why this error is happening. I’d also like to avoid legacy classes, but I guess that won’t be possible.

1 Like

Then do your script like this :

local clicked = Instance.new("BoolValue")

clicked.Parent = -- Set the parent to wherever you want

-- When mouse clicked
clicked = true
clicked = false


-- Check if clicked
clicked.Changed:Connect(function()
	if clicked then	-- If yes
		-- Do your code
	end
end)

1 Like

The issue is that you are printing before you check anything. The InputObject could be anything: a window focus, a mouse move, or a mouse click.

I’ve tried checking before printing, and the same thing happens

Well it’s a fundamental issue with checking this way. I would try again.

same issue
image

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local UserInput = game:GetService("UserInputService")
local Replicated = game:GetService("ReplicatedStorage")
local ShootService = require(Replicated.Services.ShootService)

local ShootInstance = ShootService.new({Enum.KeyCode.MouseLeftButton})

UserInput.InputEnded:Connect(function(input, gpe)
	if not gpe and input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("ended")
		
		local isHolding = script.Parent.HoldServiceHandler.GetIsHoldingFromOutsideScript:Invoke()
		local isSprinting = script.Parent.SprintServiceHandler.GetIsSprintingFromOutsideScript:Invoke()

		ShootInstance:OnInput(player.Character, isHolding, isSprinting)
	end
end)

I’ve tested it, same exact thing happens.

I did this in a new place and I couldn’t get this behavior. Might be a hardware issue or maybe you have duplicates of the script.

Show your explorer, every scripts.

Try renaming the script that you’re editing. If you see prints coming from two different
contexts environments then you know you have duplicates.

Just tried that, didn’t work.____

Try making a new place and writing the script. It might be a hardware issue or something else you’ve done in the project.

Yeah I just did that, In the new place, the issue is gone.

I’ve also had this issue in many games of mine.

I think it might be something you’re doing in a script. I highly doubt it’s a Roblox problem.

Its most likely due to the scripts name or another script. Can’t be hardware error if it works in other places

I created another script with an entirely different name, and edited the code a little bit just to test, and now instead of printing it twice, it prints it 4 times