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

No, the spawn in the workspace only, there are no folders or groups

Make a folder that will become your coin folder, like the picture shown below
aseaseasdasdftgfasdf

The folder will make it easier for the script to tell how many are there in the folder.

1 Like

Done, what changes should I do in the script?

Here is the script, hope this will work because I haven’t tested it.

First script :

local YourFolder = workspace:FindFirstChild("Your_Folder")

local 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 = 2 -- How many stuffs you want its to spawn
local MaxCapacity = 5 -- How many stuffs can it hold

local CanSpawMoreStuffs = false -- Forgor to delete this one 💀

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 HowManyStuffsInFolder = #YourFolder:GetChildren()
		
		if HowManyStuffsInFolder < MaxCapacity then
			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)
		else
			return
		end
	end
end

It is still spawning more parts than MaxCapacity

I will try to fix the problem since when I test it, it still spawn more than the “MaxCapacity”

1 Like

I deleted my post because it’s does not work, well after a bit of searching I realized that I forgot to make the cloned part’s parent to the folder :man_facepalming: And also I have make some changes.

Here is the script :

local 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 = 2 -- How many stuffs you want its to spawn
local MaxCapacity = 5 -- How many stuffs can it hold

local HowManyStuffsInFolder = 1 -- Set it to 1

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 YourFolder = workspace:FindFirstChild("Your_Folder")
		
		if HowManyStuffsInFolder <= MaxCapacity then
			print("no, it does not") -- Remove this if you don't want it
			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 = YourFolder

			task.wait(WaitTime)
		else
			return
		end
		
		HowManyStuffsInFolder = #YourFolder:GetChildren() -- Gives number { DON'T DELETE }
	end
end

Good thing is it is working but sad news is that once it spawns the amount of part which is capped, after destroy the parts, they don’t respawn

Because you didn’t reset the value of “HowManyStuffsInFolder” back to 1.

1 Like

I am testing it, and fixing what is wrong with it.

Can you show me the script that delete the parts?

local HealthGem = script.Parent

HealthGem.Touched:Connect(function(hit)

	local plr = game.Players:GetPlayerFromCharacter(hit.Parent) 

	if plr then

		hit.Parent:WaitForChild("Humanoid").Health = hit.Parent:WaitForChild("Humanoid").Health + 10
		HealthGem:Destroy()

	end
end)

I have ungrouped the part and now it is just a mesh, no models

I have tested your code, It’s fine. Can I get a short video about the stuffs not spawning?

Or can you show me the your spawn stuff script.

local Floor = workspace:FindFirstChild("CoinSpawnFloor1")
local StuffOrSomethingIDK = script:FindFirstChild("HealthGem")-- 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 = 2 -- How many stuffs you want its to spawn
local MaxCapacity = 5 -- How many stuffs can it hold

local HowManyStuffsInFolder = 1 -- Set it to 1

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 YourFolder = workspace:FindFirstChild("Boss1Health")

		if HowManyStuffsInFolder <= MaxCapacity then
			print("no, it does not") -- Remove this if you don't want it
			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 = YourFolder

			task.wait(WaitTime)
		else
			return
		end

		HowManyStuffsInFolder = #YourFolder:GetChildren() -- Gives number { DON'T DELETE }
	end
end

Change to that to this

local Floor = workspace:FindFirstChild("CoinSpawnFloor1")
local StuffOrSomethingIDK = script:FindFirstChild("HealthGem")-- 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 = 2 -- How many stuffs you want its to spawn
local MaxCapacity = 5 -- How many stuffs can it hold

local HowManyStuffsInFolder = 1 -- Set it to 1

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 YourFolder = workspace:FindFirstChild("Boss1Health")
		HowManyStuffsInFolder = #YourFolder:GetChildren() -- Gives number { DON'T DELETE }

		if HowManyStuffsInFolder <= MaxCapacity then
			print("no, it does not") -- Remove this if you don't want it
			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 = YourFolder

			task.wait(WaitTime)
		else
			return
		end
	end
end
1 Like

I found the bug, the script works fine if we delete a part before the parts reach the capacity, once they does reach it the script just breaks

It happen when you’re trying to delete them all at once right?