So I’m trying to make a tool that can do a light attack when the left mouse button is pressed and a heavy attack when the right mouse button is pressed. Is it really possible to create this?
and if it is, is there anywhere I can put this in my local script?:
--Can it be put inside of Tool.Equipped or Tool.Activated?--
Tool.Equipped:Connect(function()
IdleAnim:Play()
if Humanoid.WalkSpeed >= 23 then
Humanoid.WalkSpeed = 16
end
end)
Tool.Activated:Connect(function()
if not debounce then
debounce = true
CanDamage = true
Whoosh:Play()
if combo == 1 then
AttackAnim:Play()
combo = 2
elseif combo == 2 then
Attack2Anim:Play()
combo = 1
end
wait(DEBOUNCE_NUMBER)
debounce = false
CanDamage = false
end
end)
As Enforce said, you will have to use UserInputService to check that the given input is a right click, then you can call Tool:Activate() to manually fire the Activated event. To differentiate between attacks from left/right click, you can store the click in a variable and run different blocks of code in the Activated function based on this variable’s value. For example, this pseudocode:
local mouseButton = "" -- Outside of the scope of both functions, so both can use this variable.
UserInputService.InputBegan:Connect(function(input, gpe)
if input.UserInputType == Enum.UserInputType.MouseButton1 then -- Left mouse button
mouseButton = "LEFT"
Tool:Activate()
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then -- Right mouse button
mouseButton = "RIGHT"
Tool:Activate()
end
end)
Tool.Activated:Connect(function()
if mouseButton == "LEFT" then
-- Do light attack things here
elseif mouseButton == "RIGHT" then
-- Do heavy attack things here
end
end)
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
Mouse.Button2Down:Connect(function()
--Check if the tool is equipped
--code
end)
You can use user input service or the players mouse to detect a right-click. I also would recommend to check if the tool is equipped first before continuing with execution. Here is a example with the 1_upz’s code:
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent
Mouse.Button2Down:Connect(function()
if Tool.Equipped then
-- Code here
end
end)
Replace part of this Script with your Script, and fix the variables, so they aren’t nil.
Optional: There should be an “Unequipped” function (inside the “Activated” function): add whatever you want to it, so when you unequip the tool, it stops doing something that isn’t supposed to keep happening.
Place this Script in your Tool.
local Player = game.Players.LocalPlayer -- Define
local Tool = script.Parent
local Key -- [Left=false, Right=true]
Tool.Equipped:Connect(function()
local LeftClick
local RightClick
local Activated
local Unequipped
Activated = Tool.Activated:Connect(function()
if Key == true then
if not debounce then
debounce = true
CanDamage = true
Whoosh:Play()
if combo == 1 then
AttackAnim:Play()
combo = 2
elseif combo == 2 then
Attack2Anim:Play()
combo = 1
end
wait(DEBOUNCE_NUMBER)
debounce = false
CanDamage = false
end
end
Unequipped = Tool.Unequipped:Connect(function()
Activated:Disconnect()
Unequipped:Disconnect()
end)
end)
LeftClick = Player:GetMouse().Button1Down:Connect(function()
Key = false
Tool:Activate()
Unequipped = Tool.Unequipped:Connect(function()
LeftClick:Disconnect()
Unequipped:Disconnect()
end)
end)
RightClick = Player:GetMouse().Button2Down:Connect(function()
Key = true
Tool:Activate()
Unequipped = Tool.Unequipped:Connect(function()
RightClick:Disconnect()
Unequipped:Disconnect()
end)
end)
end)
@DevCramp , @1_upz , @AC_Starmarine
UserInputService is better. Mouse is the legacy method of UserInputService.
However, ContextActionService would be the best.
local ctx = game:GetService("ContextActionService")
local function rightClick()
-- Do whatever
end
ctx:BindAction("rightClickAction", rightClick, true, Enum.UserInputType.MouseButton2, Enum.KeyCode.ButtonA)
-- Name of the action, the function, if a mobile button is created, the click, and the button for Xbox.
-- This is for if the tool gets equipped. Use tool.Equipped:Connect(function()
ctx:SetTitle("rightClickAction", "Use tool")
-- Mobile Button for text
ctx:SetImage("rightClickAction", "rbxassetid://123")
-- Mobile Button for image
ctx:UnbindAction("rightClickAction")
-- For removing it when the tool is unequipped. Use tool.Unequipped:Connect(function()
Reasons ContextActionService is better than both UserInputService and Mouse
It has cross platform support (Xbox AND mobile )
If you were hovering on the chat, it would not activate.