How To Make It So A Weapon Only Damages When Animation Is Playing

Just like the title says, I want to know how to make it so a weapon only damages when an sword animation is playing. Here is my local script in the tool, this one handles animations.

local animations = {"3208682938","3352120414","3363060046"} -- put ur animation ids here
local CanAttack = true

script.Parent.Activated:Connect(function()
if CanAttack == true then
script.Parent.CanDamage.Value = true
local animation = Instance.new("Animation",  script.Parent.Parent)
animation.AnimationId = "http://www.roblox.com/asset?id="..animations[math.random(1, #animations)]
local chosenanim = script.Parent.Parent.Humanoid:LoadAnimation(animation)
chosenanim:Play()
CanAttack = false
wait(chosenanim.Length)
CanAttack = true
script.Parent.CanDamage.Value = false
end
end)

Script in the blade

local secondhumanoid = game.Players.LocalPlayer
--Sword script?

--Define your player
local plr = game.Players.LocalPlayer
local killer = script.Parent.Parent.Parent.Parent
--Whenever the handle touches someone
script.Parent.Touched:Connect(function(hit)
    --Check to make sure it's a player
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        --Create the tag, place it in their Humanoid
        local tag = Instance.new('ObjectValue',hit.Parent.Humanoid)
        tag.Name = 'creator'
        --Make the value your player instance
        tag.Value = killer
        --To be fair, make the tag expire.. so someone that you hit 10 minutes ago doesn't reward you a KO
        wait(3)
        tag:Destroy()
    end
end)
--End of what Karoleq needs to check out
function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 3 -- Change the amount to change the damage dealt on humanoid.
        end
end
script.Parent.Touched:Connect(onTouched)
if script.Parent.CanDamage.Value == true then

    script.Parent.CanDamage.Value = true

    secondhumanoid.Humanoid:TakeDamage(3)

end


So, how do I do that? I’m stuck on this one, so help is appreciated.
Thanks

3 Likes

Maybe try with animationtrack.IsPlaying boolean value

1 Like

How would I do that exactly? 30 hcar

script.Parent.Touched:Connect(function(hit)
    --Check to make sure it's a player
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr and AnimationTrack.IsPlaying then
        --Create the tag, place it in their Humanoid
        local tag = Instance.new('ObjectValue',hit.Parent.Humanoid)
        tag.Name = 'creator'
        --Make the value your player instance
        tag.Value = killer
        --To be fair, make the tag expire.. so someone that you hit 10 minutes ago doesn't reward you a KO
        wait(3)
        tag:Destroy()
    end
end)

You should make your animation track

1 Like

So I get this and use it to replace the old one?

I’ve made this

local secondhumanoid = game.Players.LocalPlayer
--Sword script?

--Define your player
local plr = game.Players.LocalPlayer
local killer = script.Parent.Parent.Parent.Parent
--Whenever the handle touches someone
script.Parent.Touched:Connect(function(hit)
    --Check to make sure it's a player
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        --Create the tag, place it in their Humanoid
        local tag = Instance.new('ObjectValue',hit.Parent.Humanoid)
        tag.Name = 'creator'
        --Make the value your player instance
        tag.Value = killer
        --To be fair, make the tag expire.. so someone that you hit 10 minutes ago doesn't reward you a KO
        wait(3)
        tag:Destroy()
    end
end)
--End of what Karoleq needs to check out
local CanDamage = script.Parent.Parent.CanDamage

function onTouched(hit)

        
script.Parent.Touched:Connect(onTouched)
if script.Parent.CanDamage.Value == true then

    script.Parent.CanDamage.Value = true

    secondhumanoid.Humanoid:TakeDamage(3)
    end
end


It doesn’t work though unfortunately

Why are you using a local script inside the blade? Anything done from the local script does not replicate to the server (a few exceptions though).

I would recommend only handling the animations on a separate local script and the damage on a normal script, both scripts should use the Tool.Activated function, this way you won’t need a remote event as well.

2 Likes