Stopping a loop once a folders descendants reach a certain number

local spawner = script.Parent
local coin = game.ReplicatedStorage.CoinStorage.Coin
local r = 10

local function startCoinSpawn()
while wait(1) do
	local coinClone = coin:Clone()
	local x,z = spawner.Position.X,spawner.Position.Z
	x,z = math.random(x - r,x + r),math.random(z - r,z + r)
	local pos = Vector3.new(x,spawner.Position.Y,z)
	coinClone.Parent = workspace.Coins
		coinClone.Position = pos
	end
end

Put it simply, I need to make it so that when the contents of the coins folder is equal to 10, the loop stops. Now, if someone collects a coin, it’ll disappear, and more coins will spawn in its place. I basically need the coin spawn area to always have 10 coins in it. (Of course with a wait between coin spawning)

local spawner = script.Parent
local coin = game.ReplicatedStorage.CoinStorage.Coin
local r = 10

local function startCoinSpawn()
while wait(1) do
        if #workspace.Coins:GetChildren() == 10 then
else
	local coinClone = coin:Clone()
	local x,z = spawner.Position.X,spawner.Position.Z
	x,z = math.random(x - r,x + r),math.random(z - r,z + r)
	local pos = Vector3.new(x,spawner.Position.Y,z)
	coinClone.Parent = workspace.Coins
		coinClone.Position = pos
	end
end
end

Sorry if it looks bad.

Alright so, from what I understand you want the loop to continue forever but conditionally add coins to the folder?
If that’s correct, then you can wrap the code inside of your loop block in a conditional checking if the descendants are there.

while wait(1) do
  if workspace.Coins:GetDescendants() < 10 then
    ... code here ...
  end
end

Otherwise, if you’re not trying to do that but rather wish to break that loop and have a cooldown loop restarting the startCoinSpawn() function, you could just use that conditional as GetDescendants() >= 10 or == and use the break keyword to terminate your loop.

But I’m not a fan of pointlessly looping and checking conditionals so I’ll propose a different method instead.

When a player touches (collects) a coin, you could simply make that coin invisible, change it’s position (so they appear to be randomly spawning) and make it visible after a certain period of time. This prevents you needing to do an instantiation for each and every single player that picks up a coin.

If you need further detail on my proposal, just reply, I’ll try to be active for a while.

print(string.format("Cheers,\n\t%s\t[%s]", "IDoLua", "CantBeBothered"))
>>Cheers,
>>  IDoLua  [CantBeBothered]

This is the error I get:
Workspace.Spawn Islands.Spawn Islands.Main Island.CoinSpawnAreas.Part.SpawnerScript:6: attempt to compare table < number

local spawner = script.Parent
local coin = game.ReplicatedStorage.CoinStorage.Coin
local r = 10

while wait(1) do
	if workspace.Coins:GetDescendants() < 10 then
		local function startCoinSpawn()
			while wait(1) do
				local coinClone = coin:Clone()
				local x,z = spawner.Position.X,spawner.Position.Z
				x,z = math.random(x - r,x + r),math.random(z - r,z + r)
				local pos = Vector3.new(x,spawner.Position.Y,z)
				coinClone.Parent = workspace.Coins
				coinClone.Position = pos
			end
		end
	end
end

What exactly does it mean?

I’m trying to make it so that coins will stop spawning after there is already 10 coins in the area. This means if a player collects a coin, the spawner script will restart and keep spawning coins until it reaches 10 again

if #workspace.Coins:GetDescendants() < 10 then

put a hashtag infront of workspace.Coins:GetDescendants() it counts and returns the number of values inside the table when you add it.

1 Like

Alright so this doesn’t output an error, but nothing spawns.

You aren’t calling the function startCoinSpawn

Add startCoinSpawn()

local function startCoinSpawn()
			while wait(1) do
				local coinClone = coin:Clone()
				local x,z = spawner.Position.X,spawner.Position.Z
				x,z = math.random(x - r,x + r),math.random(z - r,z + r)
				local pos = Vector3.new(x,spawner.Position.Y,z)
				coinClone.Parent = workspace.Coins
				coinClone.Position = pos
			end
		end
startCoinSpawn()--here

Try

local spawner = script.Parent
local coin = game.ReplicatedStorage.CoinStorage.Coin
local r = 10
while wait(1) do
	if #workspace.Coins:GetDescendants() < 10 then
		local function startCoinSpawn()
			while wait(1) do
				local coinClone = coin:Clone()
				local x,z = spawner.Position.X,spawner.Position.Z
				x,z = math.random(x - r,x + r),math.random(z - r,z + r)
				local pos = Vector3.new(x,spawner.Position.Y,z)
				coinClone.Parent = workspace.Coins
				coinClone.Position = pos
			end
		end
	end
startCoinSpawn()
end
Before the edit
if #workspace.Coins:GetDescendants() < 10 then

end

This should work.
EDIT: Sorry for the wrong reply.

1 Like

I’ll give this a shot in a bit

local replicated = game:GetService("ReplicatedStorage")
local coinStorage = replicated.CoinStorage
local coin = coinStorage.Coin

local spawner = script.Parent
local coinsFolder = workspace.Coins

local function startCoinSpawn()
	while true do
		task.wait(1)
		if #coinsFolder:GetDescendants() < 10 then
			local coinClone = coin:Clone()
			coinClone.Position = Vector3.new(spawner.Position.X + math.random(-10, 10), spawner.Position.Y, spawner.Position.Z + math.random(-10, 10))
			coinClone.Parent = workspace.Coins
		end
	end
end

Nah don’t worry about it, I missed the hash completely since I was tired. Also you’re trying to call startCoinSpawn() outside the context it was defined in your answer.

This script causes nothing to happen. No errors in output, nothing.

You may want to replace :GetDescendants() for :GetChildren(), you also need to call the function for it to execute, i.e; somewhere below the function’s definition you would do.

startCoinSpawn()

Still nothing. Here’s the code:

local replicated = game:GetService("ReplicatedStorage")
local coinStorage = replicated.CoinStorage
local coin = coinStorage.Coin

local spawner = script.Parent
local coinsFolder = workspace.Coins

local function startCoinSpawn()
	while true do
		task.wait(1)
		if #coinsFolder:GetChildren() < 10 then
			local coinClone = coin:Clone()
			coinClone.Position = Vector3.new(spawner.Position.X + math.random(-10, 10), spawner.Position.Y, spawner.Position.Z + math.random(-10, 10))
			coinClone.Parent = workspace.Coins
		end
	end
	startCoinSpawn()
end

By the way, I forgot to mention this, but I have 4 separate spawn area, and I have this script in each one. They each call to the same Storage Folder and Placement Folder.

You wouldn’t call the function from within the function itself.

local function startCoinSpawn()
	while true do
		task.wait(1)
		if #coinsFolder:GetChildren() < 10 then
			local coinClone = coin:Clone()
			coinClone.Position = Vector3.new(spawner.Position.X + math.random(-10, 10), spawner.Position.Y, spawner.Position.Z + math.random(-10, 10))
			coinClone.Parent = workspace.Coins
		end
	end
end

startCoinSpawn()

If functions confuse you, you can change the code for the following.

while true do
	task.wait(1)
	if #coinsFolder:GetChildren() < 10 then
		local coinClone = coin:Clone()
		coinClone.Position = Vector3.new(spawner.Position.X + math.random(-10, 10), spawner.Position.Y, spawner.Position.Z + math.random(-10, 10))
		coinClone.Parent = workspace.Coins
	end
end

Thanks. It works now. Usually I’m pretty good with functions, but I got confused by a previous post.