I made this random placement system for my Backrooms game and everything works just fine, except the repeat loop to prevent objects from clipping into one another, which just doesn’t work and the reset() function, which only removes one type of part out of the 2 test parts I’ve given it and lags studio a lot. This is my code:
bases = {}
basesFolder = game.Workspace.bases
spawnsFolder = game.Workspace.spawn
-- Self explanatory variables
spawns = {}
minCount = 0 -- These 2 refer to minimum and maximum placements
maxCount = 0 -- These 2 refer to minimum and maximum placements
partCount = 0 -- Total parts in the folder, don't modify this
spawnedParts = {} -- FOlder to store spawned parts for debug purposes.
for i, v in ipairs(spawnsFolder:GetChildren()) do
spawns[i] = v
partCount += 1
print("Part ", i, "was added to list")
end
-- Add all the objects to spawn into a list
for i, v in ipairs(basesFolder:GetChildren()) do
bases[i] = v
print("Spawn base ", i, "was added to list")
end
-- Add all the spawn bases into a list
function randPlace(rotation, minRotation, maxRotation)
for i, v in bases do
minCount = v.minValue.Value
maxCount = v.maxValue.value
-- Get minimum and maximum spaned part counts.
maxX = v.Position.X + v.Size.X / 2
minX = v.Position.X - v.Size.X / 2
maxZ = v.Position.Z + v.Size.Z / 2
minZ = v.Position.Z - v.Size.Z / 2
-- Determines the edges of the base to prevent parts from generating outside
for i=1, math.random(minCount, maxCount) do
print(i)
-- Test print statement
place = spawns[math.random(1, partCount)]:Clone()
place.Parent = game.Workspace.spawned
maxX -= place.Size.X
minX += place.Size.X
maxZ -= place.Size.Z
minZ += place.Size.Z
-- Modify edges to prevent spawned parts from sticking out
-- Make a new random part
place.Position = Vector3.new(math.random(minX, maxX), place.Size.Y / 2 + v.Size.Y / 2, math.random(minZ, maxZ))
print(place.Position)
-- Debug print statement
if rotation then
place.Orientation = Vector3.new(0, math.random(minRotation, maxRotation), 0)
end -- Randomise rotation if it's enabled
place.Anchored = true
spawnedParts[i] = place
-- Add the new part to spawned parts, this lets the deletion function find it.
place.Touched:Connect(function(collision)
repeat
place.Position = Vector3.new(math.random(minX, maxX), place.Size.Y / 2 + v.Size.Y / 2, math.random(minZ, maxZ))
until collision == not game.Workspace.spawned:GetChildren()
end) -- make that thing work later
maxX += place.Size.X
minX -= place.Size.X
maxZ += place.Size.Z
minZ -= place.Size.Z
-- Reset edges
end
end
end
function reset()
for i in spawnedParts do
spawnedParts[i]:Destroy()
end
end
Does anyone know what’s wrong?