Touched Event not working properly

What do you want to achieve?
I want to have it so whenever the brick stops touching the entity or is destroyed, it just stops damaging it.

What is the issue?
When touching the brick once, it does damage the character, yet it keeps damaging it even when the brick is deleted or the brick is not touching the character further.

What solutions have you tried so far?
I have tried 2-3 that haven’t seemed to work for me.

Here is an example of the type of code I am running:

hitPart.Touched.Connect:function((Hit)
local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
while Humanoid do
Humanoid:TakeDamage(1)
wait(0.1)
      end
end)

first check if the part that is touching is a player then do

local DAMAGE = 10
local INTERVAL = 1/30

local touchingHumanoids = {}

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		touchingHumanoids[humanoid] = true
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		touchingHumanoids[humanoid] = nil
	end
end)

while true do
	for humanoid in pairs(touchingHumanoids) do
		humanoid:TakeDamage(DAMAGE)
	end
	task.wait(INTERVAL)
end

It works! Thank you so much for the quick response and the assistance, I really appreciate it. :slight_smile:

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