How to make it so a player constantly takes damage while touching a part

You can try moving the coroutine part to the end and throwing the coroutine wrapper away?

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

You’re technically creating a new thread for .Touched and .TouchEnded events every time it fires anyway.

11 Likes