Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how do i switch the mouse button for this script?

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)

1 answer

✓ Accepted answer

Instead of using .Activated, you can use 'UserInputService'.

local tool = script.Parent.Parent --your tool
local toolActive = false
local damage = 15
local duration = 2
local damagedChars = {}
local UIS = game:GetService('UserInputService')
local equipped = false

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 onKey(Input)
	if not equipped then do return end end
	if not Input.UserInputType == Enum.UserInputType.MouseButton2 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
end

local function unequip()
	equipped = false
end

script.Parent.Touched:Connect(onTouched)
UIS.InputBegan:Connect(onKey)
tool.Equipped:Connect(equip)
tool.Unequipped:Connect(unequip)

This discussion is preserved as it was. New answers, comments, and votes are closed.