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

How do I deal AOE damage to enemies?

Asked by 4 years ago

Hi. I'm making a punch tool and I realized that it can only target one enemy at a time. How can I hit more at once? Thanks!

Here's the punch tool script:

local tool = script.Parent
local hitsound = tool:WaitForChild("Hit")
local hit1sound = tool:WaitForChild("Hit2")
local swingsound = tool:WaitForChild("Swing")
local canattack = tool:WaitForChild("CanAttack")
local plr = tool.Parent.Parent
local debounce = false
local char = plr.Character or plr.CharacterAdded:wait()
local root = plr.Character:WaitForChild("HumanoidRootPart")
local char
local hum
local idle
local swing1anim
local swing2anim
local hit
local mouse
tool.Equipped:connect(function(mouse)
    swing1animation = swing1anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
    swing2animation = swing2anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
    char = char or plr.Character or plr.CharacterAdded:wait()
    hum = hum or char:WaitForChild("Humanoid")
    idle = idle or hum:LoadAnimation(script.Idle)
    idle:Play()
    idle.Looped = true
end)
tool.Activated:Connect(function(mouse)
    if debounce == false then
        wait(.001)
        local ray = Ray.new(root.CFrame.p, root.CFrame.lookVector*4.35)
        local hit, hitposition = workspace:FindPartOnRay(ray, char)
        local choose = math.random(1,2)
        if hit then
            wait(.001)
            local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
            if not humanoid ~= nil then
                humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
            end
            if humanoid ~= nil then
                if choose == 1 then
                    debounce = true
                    swing1animation:Play()
                    swingsound:Play()
                    wait(.28)
                    hitsound:Play()
                    humanoid:TakeDamage(9)
                    wait(.33)
                    swingsound:Stop()
                    hitsound:Stop()
                    swing1animation:Stop()
                    debounce = false
                elseif choose == 2 then
                    debounce = true
                    swingsound:Play()
                    swing2animation:Play()
                    wait(.28)
                    hit1sound:Play()
                    humanoid:TakeDamage(9)
                    wait(.33)
                    swingsound:Stop()
                    hit1sound:Stop()
                    swing2animation:Stop()
                    debounce = false
                end
            else
                return
            end
        else
                if choose == 1 then
                    debounce = true
                    swingsound:Play()
                    swing1animation:Play()
                    wait(.7)
                    swingsound:Stop()
                    swing1animation:Stop()
                    debounce = false
                elseif choose == 2 then
                    debounce = true
                    swingsound:Play()
                    swing2animation:Play()
                    wait(.7)
                    swingsound:Stop()
                    swing2animation:Stop()
                    debounce = false
                end
            end
        end
end)
tool.Deactivated:connect(function()
    canattack.Value = true
end)
tool.Unequipped:Connect(function()
    idle:Stop()
end)

Answer this question