Multiple daily rewards which resets if you miss a day

I am currently working on a Daily Reward system. However I cant wrap my head around the logic of the system. When the player clicks on day 1, The system should give the player the reward, I have already solved that part. But I want the player to need to wait a day before they can unlock day 2. If they miss day 2 I want the “progress” to reset and return to 1. Could someone help me with the logic?

I do understand that this have something to do with os.time(), but I cant find any solutions!

First you want to set up a claimedReward table that has two keys: collectedAt and day. They’re quite self explanatory: collectedAt contains the timestamp for the collection of the last daily reward and day is a number that tells you which day the player is on (1, 2, 3, …).

When the player joins, get the saved claimedReward table from the DataStore, if it exists. Then save collectedAt and day somewhere inside the player, so you can access it later.

On the client, you should check which reward is supposed to be claimed now based on day and collectedAt and make some sort of visuals so that the player knows what to claim. You have to be careful though, because if more than two days have passed since collectedAt, then day is actually 1 - the reward resets after missing one day.

Now when the player tries to claim a reward, fire a RemoteFunction to the server (if you don’t already) to make sure that you handle all the logic on the server. Make sure to pass the day that the player is trying to claim. This is where you must do different checks:

  1. Check what time it is. If the current time is between collectedAt + 60 * 60 * 24 and collectedAt + 2 * 60 * 60 * 24 (which is the next day), then the reward to be collected is whatever number day has. If the current time is between collectedAt and collectedAt + 60 * 60 * 24 - 1, then return to the client that the reward is not ready to be claimed yet. Otherwise, the reward to be collected is day 1.
  2. Check if the reward that the player is trying to collect is the same as the reward that is to be collected. If it is not, return that the reward is not claimable. If it is, give the player the reward, change collectedAt to the current os.time(), change day to the next day and return success.

On the client, you can do some sort of visuals based on whatever was returned.

When the player leaves, you can create the claimedReward table with the collectedAt and day values and save it in a DataStore.

2 Likes

Hold up, there is an obvious mistake here. I’m going to fix it in a bit.

It’s fixed.
This is how I would do it. If anyone has improvements, please comment them.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.