Random placement system broken

So I’m making a system to randomly place crates over an area for my Backrooms game and have currently gotten to the point where I actually randomise the location of the new parts. My current code is this:

base = game.Workspace.genBase
spawnsFolder = game.Workspace.spawn
-- Self explanatory variables
spawns = {}
minCount = 3 -- These 2 refer to minimum and maximum placements
maxCount = 5 -- These 2 refer to minimum and maximum placements
partCount = 0 -- Total parts in the folder, don't modify this

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=1, math.random(minCount, maxCount) do
	print(i)
	-- Test print statement
	place = spawns[math.random(1, partCount)]:Clone()
	place.Parent = game.Workspace
	-- Make a new random part
end

I’ve tried a few different methods to randomise the part’s position but all of them have returned errors. Anyone know a good way?

This code should work, but you must make sure that the same part cannot be “chosen” multiple times.

base = game.Workspace.genBase
spawnsFolder = game.Workspace.spawn
-- Self explanatory variables
minCount = 3 -- These 2 refer to minimum and maximum placements
maxCount = 5 -- These 2 refer to minimum and maximum placements
-- you do not need that --partCount = 0 -- Total parts in the folder, don't modify this

--this is not necessary
--[[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

--USE this statement instead
spawns = spawnsFolder:GetChildren() 


for i=1, math.random(minCount, maxCount) do
	print(i)
	-- Test print statement
	local rand = math.random(1, #spawns) --use #spawns to always get right amount of elements in the array
	place = spawns[rand]:Clone()
	table.remove(spawns,rand) --remove part from the list, so it is not chosen again
	place.Parent = game.Workspace
	-- Make a new random part
end

Hope this helps.

I want that to happen though. Plus your code does exactly what I already could do but worse. I want the parts positions to be random within an area as well as the parts themselves

what’s inside spawnfolder?
Is it the crates that need to be spawned, or the spawnpoints where crates can be spawned?

okay, so I don’t really get this but I’ll try, could you clarify after?

local base = workspace.genBase --goes unused in this snippet
local spawnsFolder =  workspace.spawn
--please add local to local stuff, like is place used somewhere else or is it a local var?

local minCount = 3
local maxCount = 5
local spawns = spawnsFolder:GetChildren()
local count = #spawns--the length of the list

for i=1, math.random(minCount, maxCount) do
  print(i)
--seems to be getting a random crate or spawnpoint from the list.
  local place = spawns[math.random(1, count)]:Clone()
  place.Parent = workspace
end

so that’s the version refined, but I know you’re not looking for that, from what I understand (english isn’t my native language sorry, sometimes i don’t get stuff)

you choose a random crate (or spawnpoint) from the spawns folder, and you want to place it somewhere, randomly, in an area?

if this is the case, you can get the positions of the X, (,Y if you want it to float, but otherwise make it half’the crate 's size Y) and Z axis of your area like so:

and then take the X position of the x axis aligned parts, and the Z position of the z axis aligned parts,

these will represent your min and max values.

then do something like this:

local maxX = -12.5
local minX = -39.5

local minZ = 24.5
local maxZ = 51.5

local randomPart = workspace.cube

randomPart.Position = Vector3.new(math.random(minX, maxX), randomPart.Size.Y/2, math.random(minZ, maxZ))

it’s about getting the bounds of your chosen area (must be a square).
then after getting the numbers you integrate them into your actual script:

--previous vars
local minX = --your minimum x
local maxX = --your maximum x

local minZ = --your minimum Z
local maxZ = --your maximum z

for i=1, math.random(minCount, maxCount) do
  print(i)
--get target from list
  local place = spawns[math.random(1, count)]:Clone()
  place.Parent = workspace
--include position
 place.Position = Vector3.new(math.random(minX, maxX), place.Size.Y/2, math.random(minZ, maxZ))
end

if you want your part to float, you can also get your minimum and maximum Y values and replace place.Size.Y/2 with math.random(minY, maxY).

hope this helps!
(please clarify if i was wrong here…)

edit:
I’ve added some stuff to my picture so you can see from which cube you read the X and Z axis:


for that example, you get those, but you can easily see that by using the move tool, since it has the blue and red arrows, you just take both parts on the blue axis their z value, the least z value is min, the other would become max, and you do the same for x axis, I picked those three but you don’t really need to pick those, as long as you follow your move tool.

edit:
after you read the values you can delete the parts, they are just for reading.

edit:
if you want your cubes to fall down, like some games have these crate drops from the sky or whatever, you set the Y axis to something really high, like 100 and set unanchored to false, your part will spawn randomly in the area, but really high and roblox will ensure it falls down and then it looks like it is raining, crates?

1 Like

spawnFolder is the objects to spawn.