How do I detect if someone holds the mouse down when holding a tool?

I’m making a gun and wanna know if i can do this using a tool or user input service

When the player starts pressing the button, task.spawn() a new function that waits for the duration you want then checks if they are still holding it.

Not sure if this is what you’re looking for. It’s not part of the tool and the tool would need to be in your StarterPack.

--local script

local toolname = "Jetpack" -- your tool name
local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local tool = plr.Backpack:FindFirstChild(toolname) or plr.Character:FindFirstChild(toolname)
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()
	if tool.Parent == chr then
		print("button down")
	end	
end)

As far a pressing mouse while holding a gun, I’m sure there are 100’s example of that from the toolbox. Load up a gun and take a look.

You can try placing a LocalScript into your tool and type (or copy) the following code:

local UseInputService = game:GetService(“UserInputService”)

local Tool = script.Parent

local leftMouseButtonHeld = false

Tool.Equipped:Connect(function()

UserInputService.InputBegan:Connect(function(input)

if input.UserInputType == Enum.UserInputType.MouseButton1 then

leftMouseButtonHeld = true

while leftMouseButtonHeld == true do

print(“Holding…”)

end

end

end)

UserInputService.InputEnded:Connect(function(input)

if input.UserInputType == Enum.UserInputType.MouseButton1 then

leftMouseButtonHeld = false

while leftMouseButtonHeld == false do

print(“Released.”)

end

end

end)

end)
1 Like

i already figured it out ty anyways

No problem, Glad you were able to figure it out.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.