How do I add a secondary attack to my sword script?

Hey there, I’m currently working on a game and I have a sword script that I took from a video so I don’t really understand it. I want to make it so that if you press right click you do a hard attack which I already have the animation for.

Local script:

local Tool = script.Parent
local AnimationRemote = Tool:WaitForChild(“AnimationRemote”)
local Player = game:GetService(“Players”).LocalPlayer
local Character = game.Workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild(“Humanoid”)
local AnimationsIDs = {“rbxassetid://6355133963”,
“rbxassetid://6355133963”,
“rbxassetid://6355133963”,
“rbxassetid://6355133963”
}
local LoadedAnimation = {}
Tool.Equipped:Connect(function()
local Humanoid = Tool.Parent:FindFirstChildOfClass(“Humanoid”)
if Humanoid then
LoadedAnimation = {}
for i = 1, #AnimationsIDs do
local AnimationObject = Instance.new(“Animation”)
AnimationObject.AnimationId = AnimationsIDs[i]
table.insert(LoadedAnimation, Humanoid:LoadAnimation(AnimationObject))
end
end
end)
AnimationRemote.OnClientEvent:Connect(function(Number)
local Anim = LoadedAnimation[Number]
if #LoadedAnimation > 0 then
if Anim then
Anim:Play()
else
LoadedAnimation[math.random(1, #LoadedAnimation)]:Play()
end
end
end)

Server script:

local Tool = script.Parent
local Handle = Tool:WaitForChild(“Handle”)
local Trail = Tool.Blade.Trail
local AnimationRemote = Tool:WaitForChild(“AnimationRemote”)
local CanDamage = false
local Debounce = true
local Stunned = false
local MyRoot
local ComboCount = 1
local LastSwing = tick()
local Deb = {}
local PushForce = 10
local StunDuration = 0.5
local function Push(TargetRoot)
if TargetRoot then
local TowardTarget = CFrame.new(MyRoot.Position. TargetRoot.Position).LookVector
TargetRoot.Velocity = TowardTarget * PushForce
end
end
Handle.Touched:Connect(function(Hit)
local Humanoid = Hit.Parent:FindFirstChildOfClass(“Humanoid”)
local HumanoidRootPart = Hit.Parent:FindFirstChild(“HumanoidRootPart”)
if Humanoid and not Deb[Humanoid] and HumanoidRootPart and CanDamage and not Hit.Parent:IsDescendantOf(Tool.Parent) then
Deb[Humanoid] = true
Humanoid:TakeDamage(10)
Humanoid.WalkSpeed = 0
Stunned = true
wait(StunDuration)
Humanoid.WalkSpeed = 16
if MyRoot then
–Push(HumanoidRootPart)
end
end
end)
Tool.Activated:Connect(function()
if Debounce and not Stunned then
Debounce = false
CanDamage = true
Deb = {}
local ExtraDelay = 0
if tick() - LastSwing > 0.6 or ComboCount >= 4 then
if ComboCount >= 4 then
ExtraDelay = 0.5
end
ComboCount = 1
else
ComboCount = ComboCount + 1
end
AnimationRemote:FireAllClients(ComboCount)
LastSwing = tick()
Trail.Enabled = true
wait(0.3 + ExtraDelay)
CanDamage = false
Debounce = true
Trail.Enabled = false
elseif Stunned then
wait(StunDuration)
Stunned = false
end
end)
Tool.Equipped:Connect(function()
MyRoot = Tool.Parent:FindFirstChild(“HumanoidRootPart”)
end)

Btw sorry for the bad formatting

1 Like

I think you could name each animation as something different like
local AnimationOne = “rbxassetid://6355133963” – Id here
local AnimationTwo = “rbxassetid://6355133963” – Id here

then you could use a: local animationsRandom = math.random(1, #Animations)

then link it:
if animationsRandom = 1 then
–Attack
else
–Other Attack

Hope this helps!

Better formatted:

Local script:

local Tool = script.Parent
local AnimationRemote = Tool:WaitForChild("AnimationRemote")
local Player = game:GetService("Players").LocalPlayer
local Character = game.Workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild("Humanoid")

local AnimationsIDs = { 
    "rbxassetid://6355133963",
    "rbxassetid://6355133963",
    "rbxassetid://6355133963",
    "rbxassetid://6355133963"
}
local LoadedAnimation = {}

Tool.Equipped:Connect(function()
    local Humanoid = Tool.Parent:FindFirstChildOfClass("Humanoid")
    if Humanoid then
        LoadedAnimation = {}
        for i = 1, #AnimationsIDs do
            local AnimationObject = Instance.new("Animation")
            AnimationObject.AnimationId = AnimationsIDs[i]
            table.insert(LoadedAnimation, Humanoid:LoadAnimation(AnimationObject))
        end
    end
end)

AnimationRemote.OnClientEvent:Connect(function(Number)
    local Anim = LoadedAnimation[Number]
    if #LoadedAnimation > 0 then
        if Anim then
            Anim:Play()
        else
            LoadedAnimation[math.random(1, #LoadedAnimation)]:Play()
        end
    end
end)

Server script:

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Trail = Tool.Blade.Trail
local AnimationRemote = Tool:WaitForChild("AnimationRemote")

local CanDamage = false
local Debounce = true
local Stunned = false
local MyRoot
local ComboCount = 1
local LastSwing = tick()
local Deb = {}
local PushForce = 10
local StunDuration = 0.5

local function Push(TargetRoot)
    if TargetRoot then
        local TowardTarget = CFrame.new(MyRoot.Position, TargetRoot.Position).LookVector
        TargetRoot.Velocity = TowardTarget * PushForce
    end
end

Handle.Touched:Connect(function(Hit)
    local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
    local HumanoidRootPart = Hit.Parent:FindFirstChild("HumanoidRootPart")
    if Humanoid and not Deb[Humanoid] and HumanoidRootPart and CanDamage and not Hit.Parent:IsDescendantOf(Tool.Parent) then
        Deb[Humanoid] = true
        Humanoid:TakeDamage(10)
        Humanoid.WalkSpeed = 0
        Stunned = true
        wait(StunDuration)
        Humanoid.WalkSpeed = 16
        if MyRoot then
            --Push(HumanoidRootPart)
        end
    end
end)

Tool.Activated:Connect(function()
    if Debounce and not Stunned then
        Debounce = false
        CanDamage = true
        Deb = {}
        local ExtraDelay = 0
        if tick() - LastSwing > 0.6 or ComboCount >= 4 then
            if ComboCount >= 4 then
                ExtraDelay = 0.5
            end
            ComboCount = 1
        else
            ComboCount = ComboCount + 1
        end
        AnimationRemote:FireAllClients(ComboCount)
        LastSwing = tick()
        Trail.Enabled = true
        wait(0.3 + ExtraDelay)
        CanDamage = false
        Debounce = true
        Trail.Enabled = false
    elseif Stunned then
        wait(StunDuration)
        Stunned = false
    end
end)

Tool.Equipped:Connect(function()
    MyRoot = Tool.Parent:FindFirstChild("HumanoidRootPart")
end)
1 Like

No it works fine right now. I just want it to be a seperate attack than the left click which has 4 different animations (I only did one).

you could use MouseButton2Click

1 Like

Oh I am so stupid how couldn’t I think of that lmao