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

How to make TakeDamage() register only once?

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

✓ Accepted answer

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)

1 comment

0

Thanks!!!

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