The random model placement system I have been developing for my Backrooms game and currently have this code:
bases = {}
basesFolder = game.Workspace.bases
spawnsFolder = game.Workspace.spawn
-- Self explanatory variables
spawns = {}
minCount = 0 -- These 2 refer to minimum and maximum placements, don't modify
maxCount = 0 -- These 2 refer to minimum and maximum placements, don't modify
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
-- Make a new random part
maxX -= place.PrimaryPart.Size.X
minX += place.PrimaryPart.Size.X
maxZ -= place.PrimaryPart.Size.Z
minZ += place.PrimaryPart.Size.Z
-- Modify edges to prevent spawned parts from sticking out
--place.PrimaryPart.Position = Vector3.new(math.random(minX, maxX), place.PrimaryPart.Size.Y / 2 + v.Size.Y / 2 + 0.001, math.random(minZ, maxZ))
place.PrimaryPart.Position = Vector3.new(0, 0, 0)
print(place.PrimaryPart.Position) -- Debug print statement
if rotation then
place.PrimaryPart.Orientation = Vector3.new(0, math.random(minRotation, maxRotation), 0)
end -- Randomise rotation if it's enabled
place.PrimaryPart.Anchored = true
spawnedParts[i] = place -- Add the new part to spawned parts, this lets the deletion function find it.
--print(place:GetBoundingBox())
boxFrame, boxSize = place:GetBoundingBox() -- Get the part's bounding box location and size, currently returns very wrong number
print(boxFrame, " ", boxSize)
--boxSize = place:GetExtentsSize()
local exclude = OverlapParams.new()
exclude:AddToFilter({v, place:GetChildren()}) -- Parts that are children of the spawned parts and will be ignored by the GetPartBoundsInBox method
--print(exclude)
repeat
testPos = CFrame.new(math.random(minX, maxX), place.PrimaryPart.Size.Y / 2 + v.Size.Y / 2, math.random(minZ, maxZ))
--print(testPos)
--print(boxSize)
--print(#workspace:GetPartBoundsInBox(testPos, boxSize, exclude))
--print(workspace:GetPartBoundsInBox(testPos, boxSize, exclude))
--print({place, v})
task.wait()
until #workspace:GetPartBoundsInBox(testPos, boxSize, exclude) == 0
--until workspace:GetPartBoundsInBox(testPos, boxSize) == {v} or workspace:GetPartBoundsInBox(testPos, boxSize) == {place, v}
print("Location: ", testPos)
place:PivotTo(testPos)
print("Placed") --Debug print statement
maxX += place.PrimaryPart.Size.X
minX -= place.PrimaryPart.Size.X
maxZ += place.PrimaryPart.Size.Z
minZ -= place.PrimaryPart.Size.Z
-- Reset edges
end
end
end
randPlace(true, 0, 90)
And for some reason, place:GetBoundingBox returns 93.8763656616211, 7.170000076293945, 27.984848022460938 instead of the actual dimensions of the model currently being used, which are 3.5, 3.5, 3.5. I can’t manually set those since there will be models of different sizes being used when the system is ready. I have tried using GetExtentsSize instead but that also returns incorrect numbers. This is what the model looks like in the explorer in case that has an effect