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)
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
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)
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)
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 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