Touch event for kill part randomly stops working

I use a particular script for damage/kill parts, the script is you simply touch part and it takes health. I used this script for years but in a new game that I’ve recently published, it either works only first few minutes when the server is new, or does not work at all. Moreover, in studio it works absolutely fine.

The script in question:

debounce = false
script.Parent.Touched:Connect(function(hit)
	if not debounce then
	
	debounce = true
		
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
		end
	
	wait(0.5)
	
	debounce = false
	end
end)

I assume that maybe input gateway script by Maximum_ADHD causes the problem? Or collision with some other scripts? Any help or advice is much appreciated :pray:

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if debounce then return end

	local character = hit.Parent
	if not character then return end

	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		debounce = true
		humanoid:TakeDamage(10)
		task.delay(0.5, function()
			debounce = false
		end)
	end
end)

2 Likes

Thanks!! I will try it out and lyk if it solves the problem or not

Edit: it works!

the issue with this is that you didn’t account for objects other than characters hitting the kill part

1 Like

Yeah, I saw it too now, thanks! Still wondering though why this script works well in all other games where I use it…

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