I'm learning how to script and saw this in a tutorial but it repeats the damage to fast. How can I make it only hit for the "TakeDamage" once?
local sword = script.Parent -- variable to set string "sword" as the laser sword tool remember "." is indexing
local canAttack = true -- create a boolean variable
local canDamage = false
function onTouch(part)
local character = part.Parent
local player = game.Players:GetPlayerFromCharacter(character) -- GetPlayerFromCharacter is a built in function of Player class
if player then
if canDamage == true then-- "== is used to compare" where as "=" is used to set something
character.Humanoid:TakeDamage(20)
end
end
end
sword.Blade.Touched:Connect(onTouch) -- ".Touched" is indexing an event that returns the part it touches
function Attack()
local attack = sword.Parent.Humanoid:LoadAnimation(script.SwordSwing) -- variable that sets the players swordswing animation
if canAttack == true then
attack:Play() -- playes the "attack" animation that was set in the variable up above
canAttack = false
canDamage = true
wait(0.5)
canDamage = false
wait(0.25)
canAttack = true
end
end
sword.Activated:Connect(Attack)

1 comment
What I mean is I only want it to apply damage once per animation, not stack for every frame of the animation that the sword is touching the connected player.