Trying to spawn multiple parts in a singular part

I’m trying to make parts spawn randomly on those red colored spawners below. I tried pairs and it didn’t worked. Any solutions for this?

	local Banana =  coroutine.wrap(function()
		local function BananaSpawn()
			for i,v in pairs(game.Workspace.Maps:GetChildren()) do
				if v:IsA("Folder") then

					local bananapeel = game.ReplicatedStorage.Events.BananaPeel
					local clone = bananapeel:Clone()

					for _,e in pairs(v.EventsSpawner:GetChildren()) do -- spawns randomly within the part
						local areaSize = e.Size * 0.5

						local min = e.Position - areaSize
						local max = e.Position + areaSize

						bananapeel.CFrame = CFrame.new(
							math.random(min.X, max.X), 
							min.Y + bananapeel.Size.Y * 0.5,
							math.random(min.Z, max.Z)
						)
					end
					
				end
			end
		end

		local amountSpawned = 0
		local rate = 15

		repeat
			BananaSpawn()
			amountSpawned = amountSpawned + 1
		until amountSpawned == rate -- the rate on how many things to spawn
	end)

	Banana()
2 Likes

Hi there!

I am not sure how this could be done with the kind of shape you have in your image but I recently made a procedural room generation system with props and here’s how I handled the spawning of props:
(I highly recommend reading the comments)

CanPlaceProp function: (should be above the main function)

-- Function to check if a prop can be placed at a given position
function CanPlaceProp(position, room, prop)
	local ray = Ray.new(position, Vector3.new(0, 1, 0))
	local ignoreList = {prop}
	local hitPart, hitPosition, hitNormal = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

	-- Check if the ray hit something and it's not the PropBlock
        -- will explain this ^^^^^ at the end
	return hitPart == nil or (hitPart.Name ~= "PropBlock" and hitPart ~= prop)
end

Main part spawning function:

local propInstance = prop:Clone() -- change "prop" to what part you want to clone, should be in ReplicatedStorage

-- Attempt to place the prop in a suitable position
local attempts = 0
local maxAttempts = 15 -- you should probably make this number higher for your situation
local propZones = room.PropZones:GetChildren() -- This is a folder and should contain parts that are the shape of the thing you want to place your parts on, and it should be 0.1 studs thick (see image nr. 1)
repeat
	local NewRandom = Random.new()
	local propZone = propZones[NewRandom:NextInteger(1, #propZones)]
	local randomPosition = Vector3.new(
		NewRandom:NextNumber(-propZone.Size.X/2, propZone.Size.X/2) + propZone.Position.X,
		NewRandom:NextNumber(-propZone.Size.Y/2, propZone.Size.Y/2) + propZone.Position.Y,
		NewRandom:NextNumber(-propZone.Size.Z/2, propZone.Size.Z/2) + propZone.Position.Z
	)
	propInstance:SetPrimaryPartCFrame(CFrame.new(randomPosition)) -- your part should be inside a model, as well as have a small part (0.1 stud cube) at the bottom for its origin (see image nr. 2)

	attempts = attempts + 1
until CanPlaceProp(propInstance.PrimaryPart.Position, room, propInstance) or attempts >= maxAttempts

-- Check if a suitable position was found
if attempts < maxAttempts then
	propInstance.Parent = room.Propsplacement -- Change this to the folder you want the part to be in
else
	warn("Unable to find a suitable position for prop: ", PropToGenerate)
	propInstance:Destroy()
end

Images and a bit of explaining:
image nr. 1

With the shape of your platform, I recommend making a propZone the size of those platforms (or a propZone for each platform because you can use multiple, just put them in a folder) and then using PropBlock parts to block out the air and not let the parts spawn outside of the platforms (also put them in a folder, you can use as many as you want)

image nr. 2 (had to use imgur because it wasn’t letting me post it here for some reason, so I hope that the image loads correctly)

Let’s say you want random barrels to be placed, this is how you would set that up ^

I hope that all of this makes sense and that you can implement this into your script!

1 Like

Question, why does it need to be 0.1 stud thick? Is it okay if it goes beyond the limit of it. Plus, is there a way to not make the Y position of the prop goes under the map?

It can be thicker, I use 0.1 because that’s the size of the OriginPart, meaning the prop will spawn on the floor. If you make it bigger than 0.1 on the Y axis then the props will spawn in the air as well.
As for making sure the prop doesn’t go under the map, have you placed the OriginPart correctly? Unless I misunderstood your question.
Also, I forgot to mention this but if your prop is not anchored you should weld the OriginPart to everything else.

Feel free to ask me as many questions as you want and I’ll try my best to respond!

(also I am now realising that I have accidentally typed “One stud” instead of “0.1 studs” in the first image, but I’m glad you understood what I meant)

1 Like

Last question, where do you put the “PropBlock” at?

Sorry for the late response but you can put it anywhere you don’t want props to spawn (so above any parts of the PropZone(s) where you don’t want props. (they won’t do anything if they’re not above any PropZones)
Also, don’t forget to set their transparency to 1 and make sure the player can’t collide with them.
Hope this makes sense, good luck!

Alright, thank you for the help!

Hello, there’s some slight update to the PropBlock. It doesn’t remove the props or move it to the area that is not blocked with the PropBlock. The purple block is the PropBlock.

Try moving the PropBlock lower a bit maybe? It should be working.
Is the OriginPart clipping into the floor maybe? if not then you could also make the ray from the CanPlaceProp raycast go higher because maybe it’s just not reaching it:

local ray = Ray.new(position, Vector3.new(0, 2, 0)) -- Was 0, 1, 0 before, you can make it even longer if you want

Just make sure to not have the OriginPart spawn inside the PropBlock, raycasting from inside an object to find that object doesn’t work sadly, that’s why the PropBlock is higher. (The ray starts from the exact middle of the OriginPart and goes up by how many studs you told it to in the Vector3.new)

Oh and once again make sure the OriginPart’s scale is 0.1, 0.1, 0.1 and that it’s in the exact bottom middle of the prop.

Yeah the “FindPartOnRayWithIgnoreList” may have been deprecated but nevertheless I appreciate for all your help needed. Sorry if i’m wasting your time.

No don’t worry, you’re not wasting my time.
Either way, I wish you luck on your game and sorry if I gave you a headache with how complicated this system is, hope you can figure it out!

And if it still doesn’t work then please do reply to me again and I’ll send my entire room generation system just so you can look through the code and the rooms and props and see if I was stupid and forgot to send something important.

Anyways, good luck!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.