Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Delay between attacks using Touched event?

Asked by 4 years ago
Edited 4 years ago

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.

0
Use formatting User#30567 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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)
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! IAMSEAHAWK 0 — 4y
0
No problem! User#30567 0 — 4y
Ad

Answer this question