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

how do i get the sword to hit multiple enemy instead of one?

Asked by 3 years ago
Edited 3 years ago

I took a sword, because it served my game, but it ended up that it hits only one enemy at a time

http://prnt.sc/109t5hn

[Damage]

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")
Player = game.Players.LocalPlayer
Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

Create = Instance.new

DamageValues = {
    BaseDamage = 0,
    SlashDamage = 20, -- Damage
}

Damage = DamageValues.BaseDamage

BaseUrl = "http://www.roblox.com/asset/?id="

BasePart = Instance.new("Part")
BasePart.Shape = Enum.PartType.Block
BasePart.Material = Enum.Material.Plastic
BasePart.TopSurface = Enum.SurfaceType.Smooth
BasePart.BottomSurface = Enum.SurfaceType.Smooth
BasePart.FormFactor = Enum.FormFactor.Custom
BasePart.Size = Vector3.new(0.2, 0.2, 0.2)
BasePart.CanCollide = true
BasePart.Locked = true
BasePart.Anchored = false

Rate = (1 / 60)

AnimationsBin = Tool:WaitForChild("Animations")
R6Anims = AnimationsBin:WaitForChild("R6")
R15Anims = AnimationsBin:WaitForChild("R15")

Animations = {
    R6LeftSlash = {Animation = R6Anims:WaitForChild("LeftSlash"), FadeTime = nil, Weight = nil, Speed = 1.5, Duration = 0.5},
    R6RightSlash = {Animation = R6Anims:WaitForChild("RightSlash"), FadeTime = nil, Weight = nil, Speed = 1.5, Duration = 0.5},
    R6SideSwipe = {Animation = R6Anims:WaitForChild("SideSwipe"), FadeTime = nil, Weight = nil, Speed = 0.8, Duration = 0.5},
    R15LeftSlash = {Animation = R15Anims:WaitForChild("LeftSlash"), FadeTime = nil, Weight = nil, Speed = 1.5, Duration = 0.5},
    R15RightSlash = {Animation = R15Anims:WaitForChild("RightSlash"), FadeTime = nil, Weight = nil, Speed = 1.5, Duration = 0.5},
    R15SideSwipe = {Animation = R15Anims:WaitForChild("SideSwipe"), FadeTime = nil, Weight = nil, Speed = 0.8, Duration = 0.5},

}

Sounds = {
    Unsheath = Handle:WaitForChild("Unsheath"),
    Slash = Handle:WaitForChild("Slash"),
    Lunge = Handle:WaitForChild("Lunge"),
}

ToolEquipped = false

Remotes = (Tool:FindFirstChild("Remotes") or Create("Folder"){
    Name = "Remotes",
    Parent = Tool,
})
ServerControl = (Remotes:FindFirstChild("ServerControl") or Create("RemoteFunction"){
    Name = "ServerControl",
    Parent = Remotes,
})
ClientControl = (Remotes:FindFirstChild("ClientControl") or Create("RemoteFunction"){
    Name = "ClientControl",
    Parent = Remotes,
})

Handle.Transparency = 0
Tool.Enabled = true

function IsTeamMate(Player1, Player2)
    return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function TagHumanoid(humanoid, player)
    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = player
    creator_tag.Name = "creator"
    creator_tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end
function CheckTableForInstance(Table, Instance)
    for i, v in pairs(Table) do
        if v == Instance then
            return true
        end
    end
    return false
end

function DealDamage(character, damage)
    if not CheckIfAlive() or not character then
        return
    end
    local damage = (damage or 0)
    local humanoid = character:FindFirstChild("Humanoid") or character:FindFirstChild("Enemy")
    local rootpart = character:FindFirstChild("HumanoidRootPart") or character:FindFirstChild("Torso")
    if not humanoid or humanoid.Health == 0 or not rootpart then
        return
    end
    UntagHumanoid(humanoid)
    TagHumanoid(humanoid, Player)
    humanoid:TakeDamage(damage)
end

function CheckIfAlive()
    return (((Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0) and true) or false)
end

function Blow(Part)
    local PartTouched
    local HitDelay = false
    PartTouched = Part.Touched:connect(function(Hit)
        if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped or HitDelay then
            return
        end
        local RightArm = (Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand"))
        if not RightArm then
            return
        end
        local RightGrip = RightArm:FindFirstChild("RightGrip")
        if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
            return
        end
        local character = Hit.Parent
        if character == Character then
            return
        end
        local humanoid = character:FindFirstChild("Humanoid") or character:FindFirstChild("Enemy")
        if not humanoid or humanoid.Health == 0 then
            return
        end
        local player = Players:GetPlayerFromCharacter(character)
        if player and (player == Player or IsTeamMate(Player, player)) then
            return
        end
        HitDelay = true
        local TotalDamage = Damage
        DealDamage(character, TotalDamage)
        local Now = tick()
        wait(1)
        HitDelay = false
    end)
end


function Attack()
    Damage = DamageValues.SlashDamage + Player.Data.Sword.Value
    Sounds.Slash:Play()
    local SwingAnimations = ((Humanoid.RigType == Enum.HumanoidRigType.R6 and {Animations.R6LeftSlash, Animations.R6RightSlash, Animations.R6SideSwipe})
    or (Humanoid.RigType == Enum.HumanoidRigType.R15 and {Animations.R15LeftSlash, --[[Animations.R15RightSlash,]] Animations.R15SideSwipe}))
    local Animation = SwingAnimations[math.random(1, #SwingAnimations)]
    spawn(function()
        InvokeClient("PlayAnimation", Animation)
    end)
    wait(1)
end

function Activated()
    if not Tool.Enabled or not ToolEquipped or not CheckIfAlive() then
        return
    end
    Tool.Enabled = false
    Attack()
    Damage = DamageValues.BaseDamage
    Tool.Enabled = true
end

function CheckIfAlive()
    return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and RootPart and RootPart.Parent) and true) or false)
end

function Equipped()
    Character = Tool.Parent
    Player = Players:GetPlayerFromCharacter(Character)
    Humanoid = Character:FindFirstChild("Humanoid")
    RootPart = Character:FindFirstChild("HumanoidRootPart")
    if not CheckIfAlive() then
        return
    end
    Humanoid.WalkSpeed = (14)
    Sounds.Unsheath:Play()
    ToolEquipped = true
end

function Unequipped()
    if CheckIfAlive() then
        Humanoid.WalkSpeed = 16
    end
    ToolEquipped = false
end

function OnServerInvoke(player, mode, value)
    if player ~= Player or not ToolEquipped or not value or not CheckIfAlive() then
        return
    end
end

function InvokeClient(Mode, Value)
    local ClientReturn = nil
    pcall(function()
        ClientReturn = ClientControl:InvokeClient(Player, Mode, Value)
    end)
    return ClientReturn
end

ServerControl.OnServerInvoke = OnServerInvoke

Tool.Activated:connect(Activated)
Tool.Equipped:connect(Equipped)
Tool.Unequipped:connect(Unequipped)

Blow(Handle)

1 answer

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

Create a blank table, and when the sword collides with something while it's attacking, check if that thing has been hit before by looping through the table. If it hasn't, damage it and add it to the table, if it hasn't, do nothing. You can then clear the table at the end of the attack.

This is a demonstration I made. I'm not able to test it right now so it might not work but it will give you the general idea.


local debounce = false local tool = script.Parent local Handle = tool:WaitForChild("Handle") local wpnDamage = 30 local Activated = false local hitPlayers = {} -- Create a new table local function onTouch(part) if Activated and part.Parent:FindFirstChildOfClass("Humanoid") then local char = part.Parent local characterAlreadyHit = false -- This will stay false if the player isn't in the table for i, v in pairs(hitPlayers) do -- Loop through the table and set characterAlreadyHit to true if the player is in the table if char.Name == v then characterAlreadyHit = true end end if not characterAlreadyHit then -- Will deal damage to the player if they are not on the table char.Humanoid:TakeDamage(wpnDamage) table.insert(char.Name) -- Add the player to the table so they can't be hit again end end end tool.Activated:Connect(function() if not debounce then debounce = true Activated = true wait(2) Activated = false hitPlayers = {} -- After the weapon stops being able to attack, the table is cleared debounce = false end end) Handle.Touched:Connect(onTouch)
Ad

Answer this question