How do I run code at a certain time in real time?

Sorry for the very confusing title, couldn’t come up with a better one.

I’m trying to make stores that the player can purchase for some amounts of time (in my case: 1 hour, 2 hours, 6 hours, 12 hours and 24 hours). If the player purchases a store for 1 hour, they can edit the store, other players can enter it, but after 1 hour has passed, the store closes down, making other players unable to enter. I only really need help with making some code run after 1 hour has passed from the time of purchasing the store, I can make everything else myself.

Any tutorials that you know of that cover something like this? What do I need to look into?

Thanks in advance.

os time, read it and it might help with the problem you are encountering right now

2 Likes

Also if you still don’t really understand, use a sample code from the hub and play around with it in a baseplate template and see what it does, and how you can input that into your game. Have a great day (or night) :smile:

1 Like

I should be able to figure it out, thank you :sweat_smile:

1 Like

Well, I think I figured it out, but I just wanted to ask what the most efficient way to check if enough time has passed by would be. Would it be inefficient to do something like this? This starts running when an event is fired.

local purchaseTime = os.time()
while wait(60) do
	if os.time() - purchaseTime >= 3600 then
		print("Store closed")
	else
		print("Store open")
	end
end
1 Like

OS time seems to be the best way to deal with this. I’m adding on to it as I think more info is needed on how you can apply this.

If the player remains in game
You can do a simple script which will wait the time a player claimed a property before sending a signal to vacant it. Do this your prefered method (Bindable Event, etc.)

If the player leaves the server
Whenever a player claims a store, save in DataStoreService (or any data saving service of your choosing) both the os.time and the number of seconds the player should own said property.
Run a quick check to see if there is any time left for the player to own the property for (i.e. claimed a property for 6 hours, left the game and joined 1 hour later), and then run the same script as in the previous condition but with the remaining time.

Considering the check runs every minute, not really. It’s not particularly clean, but I don’t see a quick alternative to this. If it works, and isn’t inefficient, go for it.

2 Likes

Thank you for your reply, but this is not what I need :sweat_smile: I want the store to stay even when the player leaves the game, so I’ve got everything figured out then :smiley: EDIT: Ah but it struck me, I’m not sure, but will the script stop checking if enough time has passed, because the player that called the event left the game?

Considering the check runs every minute, not really. It’s not particularly clean, but I don’t see a quick alternative to this. If it works, and isn’t inefficient, go for it.

Alright, thank you :slight_smile:

If the server in which the store is present shuts down (all players leave), then all scripts will stop obviously. You would have to do some more calculations and see how much time of the store is left, and how much time has passed since the player rejoined and the store actually got ‘reopened’, to know how much money to give them.

As your script doesn’t interact with the player, then it won’t stop checking. It will just run until it’s either completed or the server shuts down.

1 Like

Okay, big thanks! I think I’ve got everything figured out, then :smile: