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

How can I get this tool to only do the damage one time instead of every tick there is connection?

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

0

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.

1 answer

Well, you just don't set it back to true/false. So here is the solution:

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
local debounce = true

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 and debounce == true then
        attack:Play() -- playes the "attack" animation that was set in the variable up above
	debounce = false
        canAttack = false

        canDamage = true
        wait(0.5)

        canDamage = false
        wait(0.25)
        canAttack = true
    end

end
sword.Activated:Connect(Attack)

I added a debounce variable so it only works once. I hope that helps!

1 comment

0

I guess I worded my problem wrong. I think that is a step in the right direction. But what I need is for it to only do the damage once per swing, so the debounce doesn't help in that situation. During the animation SwordSwing it makes contact throughout the animation and is stacking damage.

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