Combat system - hitbox

how is it recommend i limit the damage to an enemy to once per hit, currently i use a hitbox system and sometimes it damages the NPC multiple times in a hit, how is it recommended i make it do 1 per hit.

Search the forums. This has been answered many times.

Use a Debounce since the Touched event can fire many times even though it only really touches once. It basically switches a variable between true and false, and if the touched event fires again it checks if the variable has been changed. If it has it doesn’t allow any more hits until a task.wait(x # of seconds) later.

will do, my bad i didn’t see anything about it but maybe i didn’t search the right thing.

1 Like

my code looks like this

    hitbox.Touched:Connect(function(hit)
        local deb = false
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid and hit.Parent ~= character then 
            if not deb then
                local p = game.Players:GetPlayerFromCharacter(hit.Parent)
                print(damage)
                if p == nil then
                    deb = true
                    humanoid:TakeDamage(damage)        
                else
                    print("This is a player")
                end
            end
        end
    end)

it still damages multiple times in 1 punch.

A debounce needs a task.wait(time) to work properly and keep the Touched event from firing again.

local deb = false -- set it before the touched function since it hasn't been hit yet

hitbox.Touched:Connect(function(hit)
    if deb then return end    -- stops another hit from registering
        local deb = true     -- if it wasn't true then the hit should be registered
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid and hit.Parent ~= character then 
            if not deb then
                local p = game.Players:GetPlayerFromCharacter(hit.Parent)
                print(damage)
                if p == nil then   -- if there is no player then take damage?
                    -- ?  deb = true
                    humanoid:TakeDamage(damage)        
                else
                    print("This is a player")
                end
            end
        end
        task.wait(1)  -- or however long you want the debounce to last
        deb = false   -- resets it so the hit can be registered again
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.