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.
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)