How to make stock system like grow a garden?

Im trying to make a grow a garden type game but i have no clue how the stock system works. It resets every 5 minutes globally and also you cant buy any more seeds if you leave and rejoin the game if the stocks didn’t reset yet

3 Likes

I haven’t seen it so I don’t know if it’s random or based on events from servers. If it’s random just make all the prices a random function of the server time, that will make it the same across servers.

Code snippet for function that returns how much minutes until seed stock reset

local function getTimeUntilNextCycle()
    local now = os.time()
    local secondsPast = now % (5 * 60) -- 5 minutes in seconds
    local timeLeft = (5 * 60) - secondsPast
    return timeLeft
end

it returns how much seconds until the seed stock starts
Maybe to check when the stock happens u could do something like

local function getTimeUntilNextCycle()
    local now = os.time()
    local secondsPast = now % (5 * 60) -- 5 minutes in seconds
    local timeLeft = (5 * 60) - secondsPast
    return timeLeft
end

local lastTimeLeft = getTimeUntilNextCycle()

while true do
    local currentTimeLeft = getTimeUntilNextCycle()
    print(currentTimeLeft)
    if currentTimeLeft > lastTimeLeft then
        -- Stock reset here
       print("stock!!!!")
    end
    lastTimeLeft = currentTimeLeft
    task.wait(1)
end

U can use datastore service, to store what seeds u already bought, and the ones u didnt
Good luck on what ur making

2 Likes

Your code wont work because in the while loop you are constantly setting now to os.time() so setting it before the loop would work and just pass through now variable as a time but i would call it local currentTime = os.time() then getTimeUntilNextCycle(currentTime)

Revisioning @MaddyRing’s code could look something like this, then as everyone else suggests, save it to a data store when the player LEAVES and only when they leave.

local function getTimeUntilNextCycle(restockTime)
    local secondsPast = restockTime % (5 * 60) -- 5 minutes in seconds
    local timeLeft = (5 * 60) - secondsPast
    return timeLeft
end

local restockTime = os.time()
local timeLeft = getTimeUntilNextCycle(restockTime)

task.spawn(function()
  for i = timeLeft, 0, -1 do
    print(i) -- number
    task.wait(1)
    if i == 0 then
      print("Restocked")
    end
  end
end)
1 Like

Ty i didnt realize the mistake
(fixed it)

1 Like

The seeds are simple values among your others player data that you save on the datastore. If the timer reach 0 while you’re online, or is 0 when you join the game, then all these seed values got reset to give you new random ones.

As for the timer, I don’t think it’s global, I think it’s related to the player same as seed values. Every player has it’s own timer value that elapse when online and is saved among others player data on the datastore in a form of rounded unix epoch number, to be able to check how many time has elapsed since the last time you played the game.

Edit: Just remembered that the timer is server-global, because when the notification appeared to me, every others players in the server was going to the shop as well. So it may be a mix of both, a server timer for online players and a player data unix time to check when you last played the game and reset seed if you were offline for more than 5 minutes.

1 Like

Yo, ur code only makes the restock run 1 time only, it wont be ideal for a game that depends on multiple restocks

To avoid unintended stuff, i think this script is ideal from post 4