Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Problem with sword TakeDamage stacking during animation?

Can someone please help me figure out how to make this sword only do damage one time during the animation? The problem I'm having is "TakeDamage" is stacking the damage multiple times throughout the animation. I've tried adding in debounces. Perhaps all in the wrong places? Should I move the onTouch function inside the Attack function? Please help, I just want the weapon to animate and apply the damage once per swing. Thank you.

local sword = script.Parent 
local canAttack = true  
local canDamage = false


function onTouch(part)
    local character = part.Parent
    local player = game.Players:GetPlayerFromCharacter(character) 

    if player then
        if canDamage == true then
			character.Humanoid:TakeDamage(20)
        	
		end
    end
end
sword.Blade.Touched:Connect(onTouch) 

function Attack()

    local attack = sword.Parent.Humanoid:LoadAnimation(script.SwordSwing) 
    if canAttack == true then
        attack:Play()
    
        canAttack = false

        canDamage = true
        wait(0.25)
		canDamage = false
        wait(0.5)
        canAttack = true
		
	
end

end
sword.Activated:Connect(Attack)

1 answer

The easy way to fix this problem is to add table inside .Touched event so that players will be 'cached' or 'stored' after damaged and you can check if the players were recently hit (damaged) or not. The table will also be reset for next use/click.

Code:

local sword = script.Parent 
local canAttack = true  
local canDamage = false


function onTouch(part)
    local character = part.Parent
    local player = game.Players:GetPlayerFromCharacter(character) 

    local recent_hits = {} -- Table to store recently damaged players
    if player then
        if canDamage == true and not recent_hits[player.Name] then -- if canDamage and player does not exists in recent_hits table
            character.Humanoid:TakeDamage(20)
            recent_hits[player.Name] = true -- Adding Player to recent_hits table
        end
    end
end
sword.Blade.Touched:Connect(onTouch) 

function Attack()

    local attack = sword.Parent.Humanoid:LoadAnimation(script.SwordSwing) 
    if canAttack == true then
        attack:Play()

        canAttack = false

        canDamage = true
        wait(0.25)
        canDamage = false
        wait(0.5)
        canAttack = true


end

end
sword.Activated:Connect(Attack)

You should also consider adding your .Touched event inside Attack() function after attack:Play() so that the players don't get hit when the player didn't click/activated the sword.

3 comments

0

@GumzocklateE As is in the original script the player doesn't get damaged unless the sword is activated and the animation is in play. How would I reset the table after each swing?

0

@EpiXBloxorZ It should auto reset, since the table is set every time touched event is fired.

0

@GumzocklateE Okay I tested the script you provided. And the sword's TakeDamage is still stacking damaging the other humanoid for each tick or frame the sword is making contact with this player. I think the problem is the table is reset for each "touched" connect. So for the period of time the sword is making contact the table is "auto reseting" and the damage is still compounded.

This discussion is preserved as it was. New answers, comments, and votes are closed.