Help making mass item spawn system with different time amounts

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

I want to make a system that spawns different items at different times
ex.
Item1 spawns every 5 minute
Item2 spawns every 10 minute
Item3 spawns every 1 minute
(there is no item that spawns in seconds)

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

I don’t know how I would go about implementing this as I tried with tick but it didn’t work as it spawned every loop ignoring

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

I tried using the server age(tick()) - tick to see the time that has passed every minute and check
if the time / spawnTimeOfItem is greater or equal to the amount of times the item has spawned before.
Sounds bad which it probably is, checks if greater than the amount of times the item spawned before
because the time is greater every loop

The script that I used for it which doesn’t work:

local spawns = workspace:WaitForChild("Map"):WaitForChild("ItemSpawns"):GetChildren()
local ss = game:GetService("ServerStorage")
local items = ss:WaitForChild("Items"):GetChildren()

local startTick = tick()

local check = {
	["Item1"] = 1,
	["Item2"] = 1
	
	
}
local chances = {
	["Item1"] = 1, 
	["Item2"] = 1
	
	
}

local spawnAmount = { 	-- goes by minutes
	["Item1"] = 2,
	["Item2"] = 1
	
}

while task.wait(60) do
	local serverAge = tick() - startTick
	for i,v in pairs(items) do
		print(serverAge, "Server Age")
		print(spawnAmount[v.Name], " Spawn Amount")
		print(check[v.Name], " Check")
		if serverAge / spawnAmount[v.Name] >= check[v.Name] then
			check[v.Name] += 1
			print(v.Name, " has spawned")
		end
	end
end

What method should I use?

1 Like

Initially I was thinking of using coroutine loops as you could run multiple loops simultaneously with different wait times. As an example assuming you already had functions called “CreatePart” and “CreateAnotherPart”. I’d like to see what other suggestions you get as this probably isn’t the most efficient method if you’re spawning numerous items.

local loopPart = coroutine.create(function()
	while wait(10) do 
		CreatePart()
	end
end)
local loopAnotherPart = coroutine.create(function()
	while wait(20) do 
		CreateAnotherPart()
	end
end)
coroutine.resume(loopPart)
coroutine.resume(loopAnotherPart)
1 Like

That was one of the methods I was contemplating, I think that it is going to be laggy if I have a lot of items.

I will stick with it and if it starts lagging I’ll try another method.

1 Like
local function createpart()
end


local function createanotherpart()
end

local spawninfo = {
    Part = {
        LastSpawn = 0,
        Cooldown = 5*60, --5 minutes, in seconds
        Function = createpart
    },
    AnotherPart = {
        LastSpawn = 0,
        Cooldown = 10*60, --10 minutes, in seconds
        Function = createanotherpart
    },
    -- do another table for any other thing you need to spawn
}

task.spawn(function ()
    while true do
        for item, info in pairs (spawninfo) do
            if os.time() - info.LastSpawn >= info.Cooldown then
                spawninfo[item].LastSpawn = os.time()
                info.Function()
            end
        end
        task.wait(1)
    end
end)
2 Likes

never heard of os.time but after trying this it worked perfectly, thank you!
Should I start using os.time instead of tick?

os.time returns seconds from UTC time, os.tick returns seconds and milliseconds from UTC time but in local time zone. If I’m not mistaken, os.tick will be deprecated in the future.

1 Like

Alright thanks! I will probably be using os.time more than since I rarely need to detect the passing of milliseconds.

1 Like