How can I make a part spawn randomly like a coin spawner

Hi Robloxians,
I was wondering how can I make a script that spawn parts on a specific part. For example I make a part named ‘floor’ and I want parts spawn randomly or at some specific spawns on that floor part.

Any ideas on how to make the same?

Edit: I forgot to mention that my part that is meant to be spawned and cloned is a model

2 Likes

Here is my take:

local yourPart = script.Parent -- its main part
local partToSpawn = script.myPart -- part to clone
local index = 3 -- how many times to repeat
local range = 10 -- how far should it spawn from the part

for i = 1, index do
   local clonedPart = partToSpawn:Clone()
   clonedPart.Position = yourPart.Position + Vector3.new(math.random(range, -range), math.random(range, -range), math.random(range, -range))
   clonedPart.Parent = workspace
end

In the post he said that he would like it to spawn on the floor. Your script is good, it’s just that for the Y position, I would take the Y position of the floor, add half the Y size of the floor, and add a number depending on your liking. This isn’t the best way to do it but if the objects changes position an size sometimes this is the way, else the “add half the Y size of the floor” is unnecessary.

There is a useful resource called ZonePlus which has random-spawning capabilities.
ZonePlus v3

1 Like

I think the code is fine. The code may be basic but at least it worked ( I tested it ).

local Floor = workspace:FindFirstChild("Your_Floor")
local StuffOrSomethingIDK = script:FindFirstChild("Your_Stuff") -- Your desired stuff

local WaitTime = 2 -- How long it will take to spawn another stuff
local HowManyStuffs = 10 -- How many stuffs you want its to spawn

while task.wait(5) do -- Remove the While Loop if you don't want it looping over and over again
	for i = 1, HowManyStuffs do
		local newStuff = StuffOrSomethingIDK:Clone()
		newStuff.Position = Floor.Position + Vector3.new(math.random(-Floor.Size.X/2, Floor.Size.X/2), Floor.Size.Y ,math.random(-Floor.Size.Z/2, Floor.Size.Z/2)) -- Make it exactly on the floor 
		newStuff.Parent = workspace
		
		task.wait(WaitTime)
	end
end

Edit : Added wait time
Another Edit : Change it so the part will spawn on top of the floor

Hey, but my item is a model, and it doesn’t seem to be working with it. Also, should i put this script in serverscriptservice or somewhere else?

Does your model have PrimaryPart? And put it on ServerScriptService ( referring to the script )

Oh okay, and my model doesn’t have primary part

What is exact reason you’re using that model?

I am using it as a gem which heals the player on touch, the models has 2 mesh one inside and one outside and I have scripted it as if outside mesh is touched it gets destroyed and gives health to player

wait I made the outer mesh primary part

Make sure you weld it together

Anyways here is the script :

local Floor = workspace:FindFirstChild("Your_Floor")
local StuffOrSomethingIDK = script:FindFirstChild("Your_Model").YourDesiredPart -- Change the "Your Desired Part" to your desired "part" I guess

local WaitTime = 2 -- How long it will take to spawn another stuff
local HowManyStuffs = 10 -- How many stuffs you want its to spawn

while task.wait(5) do -- Remove the While Loop if you don't want it looping over and over again
	for i = 1, HowManyStuffs do
		local newStuff = StuffOrSomethingIDK:Clone()
		newStuff.Position = Floor.Position + Vector3.new(math.random(-Floor.Size.X/2, Floor.Size.X/2), 5 ,math.random(-Floor.Size.Z/2, Floor.Size.Z/2)) -- Make it exactly on the floor 
		newStuff.Parent = workspace
		
		task.wait(WaitTime)
	end
end

I’m gotta go right now

Here I guess :

local Floor = workspace:FindFirstChild("Your_Floor")
local StuffOrSomethingIDK = script:FindFirstChild("Your_Model").PrimaryPart -- Your desired model PrimaryPart I guess

local WaitTime = 2 -- How long it will take to spawn another stuff
local HowManyStuffs = 10 -- How many stuffs you want its to spawn

while task.wait(5) do -- Remove the While Loop if you don't want it looping over and over again
	for i = 1, HowManyStuffs do
		local newStuff = StuffOrSomethingIDK:Clone()
		newStuff.Position = Floor.Position + Vector3.new(math.random(-Floor.Size.X/2, Floor.Size.X/2), 5 ,math.random(-Floor.Size.Z/2, Floor.Size.Z/2)) -- Make it exactly on the floor 
		newStuff.Parent = workspace
		
		task.wait(WaitTime)
	end
end

Still it isn’t working, the part is spawning but only once

Can you give me the image of your output so I can check what is wrong with it? And also the script that I gave you?

Sorry for late reponse and also I’m using a phone right now.

Sorry for late reponse again, if you want its to spawn all at once remove the task.wait(WaitTime)

This one

task.wait(WaitTime)

Hey sorry for late from from my side too, I managed to make this script work! But the only problem is I want it to keep spawning until there are a limited amount of parts, say 5 and when there are 5 parts it won’t spawn more until one part is collected/destroyed, what should I do in that case?

Did you store them in a folder?

Nope I kept the part inside the script and script is in serverscriptservice

here is the edited script: (i mean not edited but current working script)

l

ocal Floor = workspace:FindFirstChild("CoinSpawnFloor1")
local StuffOrSomethingIDK = script:FindFirstChild("HealthGem").DiamondOuter -- Change the "Your Desired Part" to your desired "part" I guess

local WaitTime = 2 -- How long it will take to spawn another stuff
local HowManyStuffs = 10 -- How many stuffs you want its to spawn

while task.wait(5) do -- Remove the While Loop if you don't want it looping over and over again
	for i = 1, HowManyStuffs do
		local newStuff = StuffOrSomethingIDK:Clone()
		newStuff.Position = Floor.Position + Vector3.new(math.random(-Floor.Size.X/2, Floor.Size.X/2), 5 ,math.random(-Floor.Size.Z/2, Floor.Size.Z/2)) -- Make it exactly on the floor 
		newStuff.Parent = workspace

		task.wait(WaitTime)
	end
end

No, I meant where did you store them after the part cloned?