Repeating a function until a criteria is matched

I’m trying to repeat an autogeneration script for UI. It randomly generates their positions and if they are overlapping with one another it returns nil, but my script is not working and I would love to know why, I cannot figure it out.

task.spawn(function() -- Signal Creator
	while task.wait(15) do
		local count = 0
		repeat 
		
			local worked
			repeat worked = newSignal() until worked == true
			
			count += 1
		until count == 4
	end 
end)
function newSignal()
	local a = math.random(5, 95)
	local b = math.random(7, 93)
	a = a / 100
	b = b / 100
	local new = game.ReplicatedStorage.Machines.Signal:Clone()
	new.Position = UDim2.new(a, 0, b, 0)
	new.Parent = ui
	
	for i,v in ui:GetChildren() do
		if v ~= new and v.Name == "Signal" then
			local overlap = areFramesOverlapping(new.SpawnHitbox, v.SpawnHitbox)
			if overlap == true then
				new:Destroy()
				return false
			end
		end
	end
	
	new.SignalHandler.Enabled = true
	
	return true
	
end

The overlapping function is working perfectly fine, basically getting the total size of the two and if it’s larger than the distance then they are touching.

1 Like

I feel pretty stupid only just now realising I forgot to add a task.wait()…

1 Like

Still not spawning 4 btw, only spawning 2 even though it really should be spawning 4. I’ll try looking for the reason but if anyone figures it out please lmk

1 Like