Humanoid TakeDamage() on touch

I am making one tool and the part (spray) when hits the character doesnt happen only 1 time (basicly makes a lot of damage)

You can see on the script only makes 35 damage on head and 20 if hit other part but on the gif is like hit kill.

Gif:

local debounce = false

local spray = script.Parent
local sound = spray.HitSound
sound.TimePosition = 4
local sound2 = spray.FloorHit

local function player_check(otherPart)
	if not otherPart.Parent:FindFirstChild("Humanoid") then return end
	local Attacker = spray:FindFirstChild("Attacker")

	if Attacker then
		local humanoid = otherPart.Parent.Humanoid
		local character = humanoid.Parent
		local humanoidRoot = character.HumanoidRootPart

		if humanoid.Parent.Name ~= spray.Attacker.Value then	 
			if otherPart.Name == "Head" then
				sound:Play()
				sound2:Play()
				humanoid:TakeDamage(35)
			else
				sound:Play()
				sound2:Play()
				humanoid:TakeDamage(20)
			end
		end
		debounce = false
	end
end


spray.Touched:Connect(player_check)


game.Debris:AddItem(spray, 2.5)

This is happening because the spray is touching multiple parts of the body multiple times. So it’s taking more and more damage.

You are not setting debounce to true,

local db = false
if db == false then
db = true
-- your code and maybe wait()?
db = false
end
1 Like

True I made db bc I was trying to think about one cooldown but after I didnt use it bc I thought using cooldown on damage can be unless.

If you don’t make db or cooldown, it will be like the gif you sent.

Alr so I set db to true when it touch and this will not damage multiple times and the sound will still play?

If you did it correctly and put wait, yes.

Ok it works but sometimes this still make hit kill.

Okay, so I think we need to have another debounce, maybe? Can you show your new code so I can check and say something about it.

local debounce = false

local spray = script.Parent
local sound = spray.HitSound
sound.TimePosition = 4
local sound2 = spray.FloorHit

local function player_check(otherPart)
	if not debounce then
		debounce = true
		sound:Play()	
		sound2:Play()
		if not otherPart.Parent:FindFirstChild("Humanoid") then return end	
		local Attacker = spray:FindFirstChild("Attacker")
		if Attacker then
			local humanoid = otherPart.Parent.Humanoid	
			local character = humanoid.Parent	
			local humanoidRoot = character.HumanoidRootPart
			if humanoid.Parent.Name ~= spray.Attacker.Value then
				if otherPart.Name == "Head" then		
					humanoid:TakeDamage(35)	
				else
					humanoid:TakeDamage(20)
				end
				debounce = false
			end
		end
	end
end

spray.Touched:Connect(player_check)
game.Debris:AddItem(spray, 2.5)

Sorry for the delay btw