Hey guys, does anyone know how to detect if a player holds Left or Right Click on mouse? I’m trying to make a combact system, Long Hold for Heavy Attacks and Fast Clicks for Light Attacks, Any help will work
This is the code i currently got
local plr = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:Connect(function(input, ret)
if ret then
return
else
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print('Left Click')
end
if input.UserInputType == Enum.UserInputType.MouseButton2 then
print('Right Click')
end
end
end)```
just like this one, it also has a counterpart called InputEnded which checks if you let go of a keycode that a previous inputBegan registered, it’s pretty much the same so you won’t have any issues understanding it
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
Mouse.Button1Down:Connect(function()
start = tick()
end)
Mouse.Button1Up:Connect(function()
local duration = tick() - start
print("Player held left click for: " .. duration)
if duration > 3 then
warn("Player held left click for over 3 seconds.")
end
end)
There is probably a much better way to do this with UserInputService, however this worked fine for me.
local mouse = game.Players.LocalPlayer:GetMouse()
local uis = game:GetService("UserInputService")
uis.InputChanged:Connect(function(input, gpe)
if input.InputType ==Enum.InputType.Keyboardthen
while mouse.Button1Down do
--Do stuff
end
end
end)
local plr = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local startLeftClick, startRightClick, endLeftClick, endRightClick = nil, nil, nil, nil
uis.InputBegan:Connect(function(input, processed)
if processed then
return
end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
startLeftClick = tick()
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
startRightClick = tick()
end
end)
uis.InputEnded:Connect(function(input, processed)
if processed then
return
end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
endLeftClick = tick()
print("Left click held for "..endLeftClick - startLeftClick.." seconds.")
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
endRightClick = tick()
print("Right click held for "..endRightClick - startRightClick.." seconds.")
end
end)