Function runs as many times as the debounce resets?

Hello! I’m not sure why, but the clickdetector function in my script’s loop runs as many times as the debounce within resets. This doesn’t happen to any other part of the loop besides this. I’ve provided the script and a picture example below.

while task.wait() do
	print(rtc)
	if rtc then
		rtc = false
		local direc = math.random(1,4)	
		print(direc)
		local task = coroutine.create(cd)
		local taskR = coroutine.resume(task)
		for i,v in pairs(entire:GetChildren()) do
			if v.ClassName == "Model" and v.Remain.Value == true then
				local clicker = v:FindFirstChild(tostring(direc))
				clicker.BrickColor = BrickColor.new("Lime green")
				for i,part in pairs(v:GetChildren()) do
					if part.ClassName == "Part" then
						part.ClickDetector.MouseClick:Once(function()
							print(part,clicker)
							if tonumber(num) > 0 then
								if part ~= clicker then
									part.BrickColor = BrickColor.new("Really red")
									v.Remain.Value = false
								elseif part == clicker then
									clicker.BrickColor = BrickColor.new("Institutional white")
									v.Click.Value = true
								end
							elseif tonumber(num) == 0 then
								for i,part in pairs(v:GetChildren()) do
									if part.ClassName == "Part" then
										part.BrickColor = BrickColor.new("Really red")
										v.Remain.Value = false
									end
								end
							end
						end)
					end
				end
			end
		end
	end
end

1 Like

Did a little bit of tampering after this post, I’m not quite sure how I managed to make this work, but I did.

while task.wait() do
	print(rtc)
	if rtc then
		local clicker = nil
		rtc = false
		local direc = math.random(1,4)	
		print(direc)
		local run = coroutine.create(cd)
		local taskR = coroutine.resume(run)
		for i,v in ipairs(entire:GetChildren()) do
			if v.ClassName == "Model" and v.Remain.Value == true then
				v.Click.Value = false
				clicker = v:FindFirstChild(tostring(direc))
				clicker.BrickColor = BrickColor.new("Lime green")
				v.Value.Value = clicker
				print(clicker, clicker.Parent, v)
				for i,part in pairs(v:GetChildren()) do
					if part.ClassName == "Part" then
						print(part)
						part.ClickDetector.MouseClick:Once(function()
							print(part,clicker)
							print(part.Parent.Value.Value, part, part.Parent)
							if tonumber(num) > 0 then
								if part ~= part.Parent.Value.Value then
									print(part,clicker)
									part.BrickColor = BrickColor.new("Really red")
									v.Remain.Value = false
								elseif part == part.Parent.Value.Value then
									part.BrickColor = BrickColor.new("Institutional white")
									v.Click.Value = true
								end
							elseif tonumber(num) == 0 then
								for i,part in pairs(v:GetChildren()) do
									if part.ClassName == "Part" then
										part.BrickColor = BrickColor.new("Really red")
										v.Remain.Value = false
									end
								end
							end
						end)
					end
				end
			end
		end
	end
end
1 Like