How to only allow players to play minigame every 30 minutes

I want to create a way of only allowing a player to do something every 30 minutes. It needs to be datastored as well. So their default time is 0

Obbies = {
    ["Grass Obby"] = 0,
}

and so when they enter a portal, it’d start the 30 minute timer, and I’m assuming I’d use something like os.time()

PlayerData.Obbies["Grass Obby"].Value = os.time()

But then I’m unsure how to compare that os.time() to 30 minutes later? Or how to check if it’s been 30 minutes

To detect how many minutes are left you subtract the current time from the saved time it gives the amount of seconds that are passed

if (os.time() - SavedTime) => amountofsecondsin30minutes then
          print(¨30 minutes passed¨)
end

Can I just os.difftime()?

local Time = os.difftime(Obby.Value, os.time())
print(Time)

It returns negative seconds, so that will cause problems to get the time in minutes

First you do os.time() and then obby.value

Pretty sure my datastores messing up with. If I set the default data to be os.time() it changes everytime I rejoin, even tho every other value in the datastore saves

Can we see your datastore script?

Nvm I managed to fix it :+1: So this

local Seconds = os.difftime(os.time(), Obby.Value)
local Minutes = Seconds / 60

if Minutes > 30 then
    print('Can play the obby')
    Obby.Value = os.time()
else
    print('You have to wait ' .. Minutes .. ':' .. Seconds
end

EDIT Nvm, that doesn’t work :confused:

In case you still didn’t get it working, this should work.

local lastObbyRun = os.time();

if os.difftime(os.time(), lastObbyRun) >= 60 * 30  then --os.difftime() takes the second value out of the first value. 60 seconds (1 minute) * 30 (minutes) or 1800 seconds.
  print("Obby time!");
  lastObbyRun = os.time();
else
  local totalSeconds = os.difftime(os.time(), lastObbyRun);
  local seconds = totalSeconds % 60;
  local minutes = (totalSeconds - seconds) / 60;
  print(string.format("No obby for you. You need to wait %02d:%02d", minutes, seconds));
  --No obby for you. You need to wait 27:06
end;

That counts up tho. This is what I got on the client to show the countdown

while true do
	if Obbies["Grass Obby"].Value == 0 then
		Title.Text = 'Can play this obby'
		Time.Text = 'NOW'
	else
		local totalSeconds = os.difftime(os.time(), Obbies["Grass Obby"].Value)
		local seconds = totalSeconds % 60
		local minutes = (totalSeconds - seconds) / 60
		
		if minutes >= 30 then
			Title.Text = 'Can play this obby'
			Time.Text = 'NOW'
		else
			Title.Text = 'Can play this obby in'
			Time.Text = string.format("%02d:%02d", minutes, seconds)
		end
	end
	
	wait()
end

And so when the time has passed 30 minutes, it goes to ‘you can play’ and when i go to play, it starts the timer, but it’s counting up, instead of down

Not sure if your issue has been solved or not, but as for it counting down try subtracking totalSeconds from 30 minutes in seconds and then convert it to minutes + seconds

		local totalSeconds = os.difftime(os.time(), Obbies["Grass Obby"].Value)
        local CountdownSeconds = 1800 - totalSeconds
		local seconds = CountdownSeconds % 60
		local minutes = (CountdownSeconds - seconds) / 60

Fun fact that you may or may not know, In Soccer(football), the timer counts up instead of down!

3 Likes