Ad
Log in to vote
0

How to make TakeDamage() register only once?

Asked by 13 days ago

I'm trying to make a punch system that does 5 damage per hit. But for some reason it does way more than 5 damage. I know it has something to do with debounces but I already did add a debounce. I know I'm not very good with scripting. Please help, thanks much.

local UIS = game:GetService("UserInputService")

local attacking = false

llocal function leftPunch()
    --Animation
    local leftPunch = script.LeftPunch
    local leftPunchTrack = humanoid:LoadAnimation(leftPunch)
    local leftFist = character:WaitForChild("Left Arm")

    if not LPcooldown then
        LPcooldown = true

        attacking = true

        leftPunchTrack:Play()
        wait(2)

        LPcooldown = false
    end 

    --Damage
    leftFist.Touched:Connect(function(hit)
        local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
        if humanoid and attacking then
            humanoid:TakeDamage(5)

            wait() --debounce
            attacking = false
        end
    end)
end

1 answer

Log in to vote
0
Answered by 13 days ago

You need to make sure you define that attacking is false before the damage

like this:

    leftFist.Touched:Connect(function(hit)
        local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
        if humanoid and attacking then
            attacking = false -- defines false before damage so it then won't attack.
            humanoid:TakeDamage(5)

            wait()
            -- add debounce here
        end
    end)
0
Thanks!!! Dehydrocapsaicin 20 — 13d
Ad

Answer this question