Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

can somebody help me make this script so that i can use mouse2 to activate the script?

Asked by 5 years ago
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)
0
nest a Mouse.Button2Down event inside a Tool.Equipped event Gey4Jesus69 2705 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)

0
Explain the script. yHasteeD 1819 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
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!

0
Explain the script. yHasteeD 1819 — 5y
0
we were talking in the chat, thats why i didnt Gey4Jesus69 2705 — 5y

Answer this question