local tool = script.Parent.Parent --your tool local toolActive = false local damage = 15 local duration = 2 local damagedChars = {} local function checkIfDamaged(x) for index, value in pairs(damagedChars) do if value == x then return true end end return false end local function onTouched(hit) if not toolActive then do return end end local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid") if Humanoid and not checkIfDamaged(Humanoid.Parent.Name) then Humanoid:TakeDamage(damage) table.insert(damagedChars, Humanoid.Parent.Name) end end local function onActivated() if toolActive then do return end end toolActive = true; wait(duration); toolActive = false damagedChars = {} end script.Parent.Touched:Connect(onTouched) tool.Activated:Connect(onActivated)
You can use the :GetMouse()
function of local player
to get the mouse class of player. Then use the Button2Down
event of mouse to get the right-click input from user.
local tool = script.Parent.Parent --your tool local toolActive = false local damage = 15 local duration = 2 local damagedChars = {} local equipped = false local mouse = game:GetService('Players').LocalPlayer:GetMouse() local function checkIfDamaged(x) for index, value in pairs(damagedChars) do if value == x then return true end end return false end local function onTouched(hit) if not toolActive then do return end end local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid") if Humanoid and not checkIfDamaged(Humanoid.Parent.Name) then Humanoid:TakeDamage(damage) table.insert(damagedChars, Humanoid.Parent.Name) end end local function onActivated() if not equipped then do return end end if toolActive then do return end end toolActive = true; wait(duration); toolActive = false damagedChars = {} end local function equip() equipped = true damagedChars = {} end local function unequip() equipped = false end script.Parent.Touched:Connect(onTouched) mouse.Button2Down:Connect(onActivated) tool.Equipped:Connect(equip) tool.Unequipped:Connect(unequip)
local player = game:GetService("Players").LocalPlayer local mouse = player:GetMouse() local tool = script.Parent.Parent local CanFire = false tool.Equipped:Connect(function() CanFire = true end) tool.Unequipped:Connect(function() CanFire = false end) mouse.Button2Down:Connect(function() if CanFire == true then --do code end end)
You could also do:
local player = game:GetService("Players").LocalPlayer local mouse = player:GetMouse() local tool = script.Parent.Parent tool.Equipped:Connect(function() mouse.Button2Down:Connect(function() --do code end) end)
But I'm always wary of doing this because I'm not sure how it responds when you unequip. You could test though!