How to stop and restart a portion of a script?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to be able to spawn items until there are 3 items. Then once there are less than 3 items it spawns some again

  1. What is the issue? Include screenshots / videos if possible!

I have gotten the items to spawn at the position that I want them too, but I don’t know how to implement the stopping and drestarting of the script.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I looked on the Dev hub and could not find the solution I was looking for

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

The minimum and max timer numbers of 1 are just there to make testing easier and quicker

local Max_Number = 1
local Min_Number = 1
local TMax_Number = 10
local TMin_Number = 1

local item1 = game.ServerStorage.Items.Roka
local item2 = game.ServerStorage.Items.SArrow
local item3 = game.ServerStorage.Items.RArrow
local item4 = game.ServerStorage.Items.Diary

local item1h = game.ServerStorage.Items.Roka.Handle 
local item2h = game.ServerStorage.Items.SArrow.Handle
local item3h = game.ServerStorage.Items.RArrow.Handle
local item4h = game.ServerStorage.Items.Diary.Handle
local spawnloc = script.Parent.Position
local children = script.Parent:GetChildren()
local itemsSpawned = #children -- since I couldn't compare children with a > statement, I had to make one equal how many children
local waited = 0

local RandomTime = math.random(Min_Number, Max_Number) --makes you wait a random amount of time

print(RandomTime)

if RandomTime > 0 then  --once you have the random time value it moves on with the script
	waited = 1 --helps to see where the script isn't working
	print(waited) --helps to see where the script isn't working
end

wait(RandomTime)  --makes you wait for that random amount of time

waited = 2 --helps to see where the script isn't working
print(waited) --helps to see where the script isn't working

if waited == 2 then -- only works after you waited for the random time
	local ItemSpawn = math.random(TMin_Number, TMax_Number) --grabs a random number to see which item will spawn
	waited = 0 --resets debug counter
	print(waited)

	local itemTable = {    --Shows what item you get depending on the number
		[1] = {item = item1, handle = item1h},
		[2] = {item = item1, handle = item1h},
		[3] = {item = item1, handle = item1h},
		[4] = {item = item1, handle = item1h},
		[5] = {item = item2, handle = item2h},
		[6] = {item = item2, handle = item2h},
		[7] = {item = item2, handle = item2h},
		[8] = {item = item2, handle = item2h},
		[9] = {item = item3, handle = item3h},
		[10] = {item = item4, handle = item4h},
	}

	local itemToSpawn = itemTable[ItemSpawn] --shows which item to spawn
	itemToSpawn.item:Clone() --clones the item from server storage
	itemToSpawn.item.Parent = workspace.spawnedItems --Sets the parent to the spawned items folder in workspace
	itemToSpawn.handle.Position = spawnloc -- set's the handles current position to the script parent location
end

2 Likes

Put it in a while loop then make a variable called “amountOfItems” add to the variable every time you make a part if “amountOfItems” < 3 then destroy the parts then loop again.

Coroutine! coroutine | Roblox Creator Documentation

I wasn’t quite sure what to do so I did this. I’m not quite sure what you meant by “destroy the parts then loop again” does that mean that it would get rid of the current spawned items? I want it to only resume once a player picks up an item thus getting rid of it from the spawnedItems folder.

local Max_Number = 1
local Min_Number = 1
local TMax_Number = 10
local TMin_Number = 1

local item1 = game.ServerStorage.Items.Roka
local item2 = game.ServerStorage.Items.SArrow
local item3 = game.ServerStorage.Items.RArrow
local item4 = game.ServerStorage.Items.Diary

local item1h = game.ServerStorage.Items.Roka.Handle 
local item2h = game.ServerStorage.Items.SArrow.Handle
local item3h = game.ServerStorage.Items.RArrow.Handle
local item4h = game.ServerStorage.Items.Diary.Handle
local spawnloc = script.Parent.Position
local children = script.Parent:GetChildren()
local itemsSpawned = #children -- since I couldn't compare children with a > statement, I had to make one equal how many children
local waited = 0
local amountOfItems = itemsSpawned - 1
local RandomTime = math.random(Min_Number, Max_Number) --makes you wait a random amount of time

while amountOfItems < 4 do
	print(RandomTime)
	print(amountOfItems)

	if RandomTime > 0 then  --once you have the random time value it moves on with the script
		waited = 1 --helps to see where the script isn't working
		print(waited) --helps to see where the script isn't working
	end

	wait(RandomTime)  --makes you wait for that random amount of time

	waited = 2 --helps to see where the script isn't working
	print(waited) --helps to see where the script isn't working

	if waited == 2 then -- only works after you waited for the random time
		local ItemSpawn = math.random(TMin_Number, TMax_Number) --grabs a random number to see which item will spawn
		waited = 0 --resets debug counter
		print(waited)

		local itemTable = {    --Shows what item you get depending on the number
			[1] = {item = item1, handle = item1h},
			[2] = {item = item1, handle = item1h},
			[3] = {item = item1, handle = item1h},
			[4] = {item = item1, handle = item1h},
			[5] = {item = item2, handle = item2h},
			[6] = {item = item2, handle = item2h},
			[7] = {item = item2, handle = item2h},
			[8] = {item = item2, handle = item2h},
			[9] = {item = item3, handle = item3h},
			[10] = {item = item4, handle = item4h},
		}

		local itemToSpawn = itemTable[ItemSpawn] --shows which item to spawn
		itemToSpawn.item:Clone() --clones the item from server storage
		itemToSpawn.item.Parent = workspace.spawnedItems --Sets the parent to the spawned items folder in workspace
		itemToSpawn.handle.Position = spawnloc -- set's the handles current position to the script parent location
	end
end

So you want the items to be in a table then remove an item from the table once a player picks it up?

The problem here is your loop will keep stopping once the items spawned reaches 4. It wants to look more like this:

while true do
    local itemsSpawned = workspace.itemspawned:GetChildren()
    if #itemsSpawned < maxItems then
        -- Code to spawn new item
        --could include a repeat loop to bring the total up to the max items without waiting.
    end
    task.wait(waittime)
end

(Please note pseudocode, may have mixed up variable names from your script)

Use bindables or remotes (if multiple players are involved). When receiving the event begin the loop.

Functions are something that are used for “reusing” a portion of code. Just use functions. Make functions of code you want to repeat, and just call the function. Too simple and basic.

I tried to use the functions but after I tried to use it, it stopped spawning items so I’m not quite sure what happened. It also didn’t give me an error msg

local Max_Number = 1
local Min_Number = 1
local TMax_Number = 10
local TMin_Number = 1

local item1 = game.ServerStorage.Items.Roka
local item2 = game.ServerStorage.Items.SArrow
local item3 = game.ServerStorage.Items.RArrow
local item4 = game.ServerStorage.Items.Diary

local item1h = game.ServerStorage.Items.Roka.Handle 
local item2h = game.ServerStorage.Items.SArrow.Handle
local item3h = game.ServerStorage.Items.RArrow.Handle
local item4h = game.ServerStorage.Items.Diary.Handle
local spawnloc = script.Parent.Position
local RandomTime = math.random(Min_Number, Max_Number) --makes you wait a random amount of time
local canSpawn = false
local itemsSpawned = script.Parent:GetChildren()



local function spawnCheck ()
	if #itemsSpawned < 4 then
		local canSpawn = true
	else
		local canSpawn = false
	end
end



local function spawnItem ()
	if canSpawn == true then
		wait(RandomTime)  --makes you wait for that random amount of time
		local ItemSpawn = math.random(TMin_Number, TMax_Number) --grabs a random number to see which item will spawn
		local itemTable = {    --Shows what item you get depending on the number
			[1] = {item = item1, handle = item1h},
			[2] = {item = item1, handle = item1h},
			[3] = {item = item1, handle = item1h},
			[4] = {item = item1, handle = item1h},
			[5] = {item = item2, handle = item2h},
			[6] = {item = item2, handle = item2h},
			[7] = {item = item2, handle = item2h},
			[8] = {item = item2, handle = item2h},
			[9] = {item = item3, handle = item3h},
			[10] = {item = item4, handle = item4h},
		}

		local itemToSpawn = itemTable[ItemSpawn] --shows which item to spawn
		itemToSpawn.item:Clone() --clones the item from server storage
		itemToSpawn.item.Parent = workspace.spawnedItems --Sets the parent to the spawned items folder in workspace
		itemToSpawn.handle.Position = spawnloc -- set's the handles current position to the script parent location
	end
end

while true do
	wait()
	spawnCheck()
	spawnItem()
end

Instead of stopping the loop every time the item count goes above a specific number you should do add something into the loop such as:

while true do

-- item script

   repeat 
      wait()
   until ItemCount <= 3

end

This just waits indefinitely until an item is picked up assuming you know how to code that in.

Hi, thanks for trying to help but, I tried your solution and it still was not spawning items once the number of children was below a certain number. I noticed something odd though, after the first couple of seconds the script breaks. If I do it with 1 second it only spawns 2 items before breaking. If I do 10 it never spawns any and I’m not quite sure how to fix that.

local Max_Number = 1
local Min_Number = 1
local TMax_Number = 10
local TMin_Number = 1

local item1 = game.ServerStorage.Items.Roka
local item2 = game.ServerStorage.Items.SArrow
local item3 = game.ServerStorage.Items.RArrow
local item4 = game.ServerStorage.Items.Diary

local item1h = game.ServerStorage.Items.Roka.Handle 
local item2h = game.ServerStorage.Items.SArrow.Handle
local item3h = game.ServerStorage.Items.RArrow.Handle
local item4h = game.ServerStorage.Items.Diary.Handle
local spawnloc = script.Parent.Position
local RandomTime = math.random(Min_Number, Max_Number) --makes you wait a random amount of time
local canSpawn = false
local itemsSpawned = script.Parent:GetChildren()
local ItemCount = #itemsSpawned - 1

wait(10)

while true do
	wait(RandomTime)  --makes you wait for that random amount of time
	local ItemSpawn = math.random(TMin_Number, TMax_Number) --grabs a random number to see which item will spawn
	local itemTable = {    --Shows what item you get depending on the number
		[1] = {item = item1, handle = item1h},
		[2] = {item = item1, handle = item1h},
		[3] = {item = item1, handle = item1h},
		[4] = {item = item1, handle = item1h},
		[5] = {item = item2, handle = item2h},
		[6] = {item = item2, handle = item2h},
		[7] = {item = item2, handle = item2h},
		[8] = {item = item2, handle = item2h},
		[9] = {item = item3, handle = item3h},
		[10] = {item = item4, handle = item4h},
	}

	local itemToSpawn = itemTable[ItemSpawn] --shows which item to spawn
	itemToSpawn.item:Clone() --clones the item from server storage
	itemToSpawn.item.Parent = workspace.spawnedItems --Sets the parent to the spawned items folder in workspace
	itemToSpawn.handle.Position = spawnloc -- set's the handles current position to the script parent location
	print("Spawned an item")
	repeat
		wait()
	until ItemCount <= 3
end

try instead of just doing wait() do wait(1) or put any number as id assume timing it to the millisecond isn’t that important for you.

Also I think I found the problem. When you set ItemCount originally so that it sets to the number of the table of THAT TIME. so for example when the game starts the ItemCount is 3 or whatever then when you do

local ItemCount = #itemsSpawned - 1

it sets the ItemCount to 3 and it doesn’t change when the table changes. What you should do is in the loop itself you check it something like this

repeat
   wait(1)
until #script.Parent:GetChildren()  <= 3

This makes it so that it constantly checks the number of items rather than just getting the number from the beginning

thx but still after a couple of seconds for whatever reason the script breaks for what seems like no apparent reason and I have it the exact same as what you told me to put. I also tested with another script just to make sure that all scripts weren’t freezing, but no it’s only this one that freezes

	repeat
		wait(1)
	until #script.Parent:GetChildren()  <= 3
end

at the moment my entire script looks like this

local Max_Number = 1
local Min_Number = 1
local TMax_Number = 10
local TMin_Number = 1

local item1 = game.ServerStorage.Items.Roka
local item2 = game.ServerStorage.Items.SArrow
local item3 = game.ServerStorage.Items.RArrow
local item4 = game.ServerStorage.Items.Diary

local item1h = game.ServerStorage.Items.Roka.Handle 
local item2h = game.ServerStorage.Items.SArrow.Handle
local item3h = game.ServerStorage.Items.RArrow.Handle
local item4h = game.ServerStorage.Items.Diary.Handle
local spawnloc = script.Parent.Position
local RandomTime = math.random(Min_Number, Max_Number) --makes you wait a random amount of time

while true do
	wait(RandomTime)  --makes you wait for that random amount of time
	local ItemSpawn = math.random(TMin_Number, TMax_Number) --grabs a random number to see which item will spawn
	local itemTable = {    --Shows what item you get depending on the number
		[1] = {item = item1, handle = item1h},
		[2] = {item = item1, handle = item1h},
		[3] = {item = item1, handle = item1h},
		[4] = {item = item1, handle = item1h},
		[5] = {item = item2, handle = item2h},
		[6] = {item = item2, handle = item2h},
		[7] = {item = item2, handle = item2h},
		[8] = {item = item2, handle = item2h},
		[9] = {item = item3, handle = item3h},
		[10] = {item = item4, handle = item4h},
	}

	local itemToSpawn = itemTable[ItemSpawn] --shows which item to spawn
	itemToSpawn.item:Clone() --clones the item from server storage
	itemToSpawn.item.Parent = workspace.spawnedItems --Sets the parent to the spawned items folder in workspace
	itemToSpawn.handle.Position = spawnloc -- set's the handles current position to the script parent location
	print("Spawned an item")
	repeat
		wait(1)
	until #script.Parent:GetChildren()  <= 3
end

is there anything else under the script? like things that are not items because that could interfere with the item count. Another thing that I can see that might cause some problems is your minimum and maximum number. When you put a math.random and make them both 1 it could be the cause of your script breaking. Other than that I can’t see any other reason besides maybe your models or something you have done prior

There is nothing else that is a child of the script parent until it spawns items. I tested it with different numbers and it didn’t function at all. I will try this in a blank project and see if that fixes the problem.
brb in a few

Ok, I tried it in a baseplate and it still has the same problem, it just stops functioning after a couple of seconds. I put the min time as 5 seconds and max at 10 seconds, I had a timer and I waited about 30 seconds and nothing happened.

is there any error message? im trying this in my own studio and its working fine

no there is no error msg it’s doing nothing.

It isn’t even getting past line 19 which is

wait(RandomTime)

I figured that out since right after that I put the line

print("waited")