Cooldown not working properly

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Making a tower defense game with a friend and we are making a tower that makes the zombie take damage

  2. What is the issue? Include screenshots / videos if possible!
    The script works fine when the wait is something low (in this case .3) but when you change it to a larger number like 10, it checks if a zombie is in the radius every 10 seconds instead of having a cooldown of 10 seconds

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried adding a Debounce but got infinite yield and also tried moving the wait down but got the same problem

(It was very hard wording this so any help is very much appreciated)

local detected = false
local TOWER_RAD = script.Parent.Detection_Radius

while wait() do
	local obj_List = TOWER_RAD:GetTouchingParts()

	for _, v in pairs(obj_List) do
		obj_List = TOWER_RAD:GetTouchingParts()
		if v.Name == "zombie" then
			detected = true
			while v:WaitForChild("health").Value > 0 and table.find(obj_List, v) do
				wait(0.3)
				v:WaitForChild("health").Value -= 1
				obj_List = TOWER_RAD:GetTouchingParts()
			end
		end
	end

	if detected == true then
		print("Zombie Detected")
	end

	detected = false
end

I believe it’s because the while loop inside another while loop pauses it, to fix this yo can use coroutine.wrap()

local detected = false
local TOWER_RAD = script.Parent.Detection_Radius

while wait() do
	local obj_List = TOWER_RAD:GetTouchingParts()

	for _, v in pairs(obj_List) do
		obj_List = TOWER_RAD:GetTouchingParts()
		if v.Name == "zombie" then
			detected = true
			coroutine.wrap(function()
while v:WaitForChild("health").Value > 0 and table.find(obj_List, v) do
				wait(0.3)
				v:WaitForChild("health").Value -= 1
				obj_List = TOWER_RAD:GetTouchingParts()
			end
               end)()
		end
	end

	if detected == true then
		print("Zombie Detected")
	end

	detected = false
end

This didn’t 100% work because there was still an infinite yield problem but my friend got it working and this definitely put us in the right direction. Thank you!