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

can somebody help me make this script so that i can use mouse2 to activate the 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 comment

0

nest a Mouse.Button2Down event inside a Tool.Equipped event

2 answers

✓ Accepted answer

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)

1 comment

0

Explain the script.

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!

2 comments

0

Explain the script.

0

we were talking in the chat, thats why i didnt

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