I have a script that spawns enemies in random positions. But they occasionally spawn inside other objects or on top of other enemies. How do I handle this problem?
local enemies = game:GetService("ReplicatedStorage").NPC.Enemies local clonePlace = workspace.NPC.Enemies local enemycount = script.Parent.EnemyCount local enemytype = script.Parent.EnemyType local maxnumber = script.Parent.MaxNumber local delaytime = script.Parent.Delay local increment = script.Parent.Increment local spawner = script.Parent while wait(delaytime.Value) do if enemycount.Value < maxnumber.Value then enemycount.Value = enemycount.Value + 1 local clonedEnemy = enemies:FindFirstChild(enemytype.Value):Clone() clonedEnemy.Parent = clonePlace local minX = spawner.Position.X - increment.Value local maxX = spawner.Position.X + increment.Value local minZ = spawner.Position.Z - increment.Value local maxZ = spawner.Position.Z + increment.Value clonedEnemy.Head.CFrame = CFrame.new( math.random(minX, maxX), spawner.Position.Y + 15, math.random(minZ, maxZ) ) end end