Spawning system doesn't seem right

Hey! I am making a simulator, and I am trying to make a spawn system like pet sim x/99.
What I am trying to do is make sure that an object NEVER spawns within these radius’s, as seen below. Only problem is, I don’t think this system works very well. It screws up very often, and is usually never right.
image

Is this the best way to do it?

local function findPossiblePosition(land)
	print(land)
	local landWorkspace = workspace:FindFirstChild("Zones"):FindFirstChild(land)
	local spawn = landWorkspace:FindFirstChild("Spawn")
	local spawnPos = spawn.Position
	local randomX = math.random(spawnPos.X - (spawn.Size.X/2), spawnPos.X + (spawn.Size.X/2))
	local randomZ = math.random(spawnPos.Z - (spawn.Size.Z/2), spawnPos.Z + (spawn.Size.Z/2))
	local randomPos = Vector3.new(randomX, spawnPos.Y, randomZ)
	for i, v in pairs(workspace.Ores:FindFirstChild(land):GetChildren()) do
		if v:IsA("BasePart") then
			local ore = v
			local origin = v:FindFirstChild("Origin")
			local magnitude = (randomPos - origin.Position).Magnitude

			if magnitude < (ore.Size.X/2) + 6 and magnitude < (ore.Size.Z/2) + 6 then
				-- If the condition is not satisfied, continue the loop
				return findPossiblePosition(land)
			end
		end
	end

	-- If no ore is found within the specified range, return the random position
	return randomPos
end

If you have any other ideas, or better ways, please let me know!

Oh yeah and if your system works I will pay u 1.5K robux (no tax) and put u in the credits of the game as a helper!

do if a part spawns in the blacklisted circles, just redo the operation until it doesn’t

That is what I am trying to do, however I don’t know how I would do that. Do you have an idea?

i’ve worked with a system like this before, wrap this in a while loop but remember to implement a retry limit so it doesn’t hang in some cases

let me know how this works, you can remove “–!strict” from the top of the script and the type annotations (for example Instance::type) if you want since i just use that for typechecking purposes

use the second argument retryLimit to specify your own retry limit otherwise it defaults to 10

--!strict
local function findPossiblePosition(land:string, retryLimit:number?): Vector3?
	print(land)
	local zones = workspace:FindFirstChild("Zones")
	if zones then
		local landWorkspace = zones:FindFirstChild(land) -- i don't know what type this is but it should work
		local spawn = landWorkspace:FindFirstChild("Spawn")::BasePart
		local ores = workspace:FindFirstChild("Ores") -- or this
		local landInstance = ores:FindFirstChild(land)
		if spawn and ores and landInstance then -- Check if these instances exist
			for i = 1, retryLimit or 10 do
				local spawnPos = spawn.Position
				local randomX = math.random(spawnPos.X - (spawn.Size.X/2), spawnPos.X + (spawn.Size.X/2))
				local randomZ = math.random(spawnPos.Z - (spawn.Size.Z/2), spawnPos.Z + (spawn.Size.Z/2))
				local randomPos = Vector3.new(randomX, spawnPos.Y, randomZ)
				for i, v in landInstance:GetChildren() do
					if v:IsA("BasePart") then
						local ore = v
						local origin = v:FindFirstChild("Origin")::BasePart
						if origin then
							local magnitude = (randomPos - origin.Position).Magnitude

							if magnitude < (ore.Size.X/2) + 6 and magnitude < (ore.Size.Z/2) + 6 then
								-- If the condition is not satisfied, continue the loop
								return findPossiblePosition(land)
							end
						else
							warn(string.format('Part %s has no origin, skipping over it', v.Name))
							continue
						end
					end
				end

				-- If no ore is found within the specified range, return the random position
				return randomPos
			end
		else
			error('A required instance was missing')
		end
	end
	return nil
end

Hey! Yeah I understand that. However, sometimes it spawns IN other blocks.

Hey, thanks for the reply. I’ll try that when I get back home! I’ll let u know if it works. Do u have a discord?

yeah, i can post it in dms if you’d like

Yea I would appreciate that a lot. What is your dsc?

i sent it to you in dms, check there

Ah okay. I’ll go check that out right now!

Uh if this doesn’t work, an solution would be to make a part as the radius and if the spawning part is touching that radius, then it won’t spawn.