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.
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!
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