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

Delay between attacks using Touched event?

Hello i am using the .Touched event to deal damage to players from a npc

here is my code

local HumanoidRootPart = script.Parent.HumanoidRootPart

local attacking = false

local function DamageFunc(humanoid)
	print("runnning")
	wait(1)
	humanoid.Health = humanoid.Health - 10
	attacking = false
end

local function Damage(player)
	local h = player.Parent:FindFirstChild("Humanoid")
	if h and attacking == false then
		DamageFunc(h)
		attacking = true
	end
end

HumanoidRootPart.Touched:Connect(Damage)

But how would one delay an attack so that it only calls after the first attack finished? because the code i use now either just constantly attacks or stacks up.

1 comment

0

Use formatting

1 answer

✓ Accepted answer

Debounce is a common way to stop multiple things from happening at once. You used it correctly, but you put the variable on the wrong place.

local HumanoidRootPart = script.Parent.HumanoidRootPart

local attacking = false

local function DamageFunc(humanoid)
    print("runnning")
    wait(1)
    humanoid.Health = humanoid.Health - 10
    attacking = false
end

local function Damage(player)
    local Humanoid = script.Parent:FindFirstChild("Humanoid")
    if Humanoid and attacking == false then
        attacking = true
        DamageFunc(Humanoid)
    end
end

HumanoidRootPart.Touched:Connect(Damage)

2 comments

0

wow thanks for your help, ill put more effort into trying to find the answer of my own next time! thanks for your help!

0

No problem!

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