You can write your topic however you want, but you need to answer these questions:
- 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
- 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.
- 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