How to make a Daily Reward System? | Tutorial

In this tutorial I’m going to show you how to make a daily reward system, ima try to make it as short as possible, really. If something does not work you can always ask me in the replies of this topic or via private message.

What do you need? (What skills)
You need to know some basic scripting things, that’s it!

How are we doing this?
We will create a DataStore especially for Daily Reward system that will save the last login time.
If a player joins a server, and the last saved time is more than 24h in the past it will give out the login bonus.
If a player joins a server, and the last saved time is not more than 24h in the past, it won’t do anything.

Tutorial

Step 1.
First, we need to call our DataStore:

local DataStoreService = game:GetService("DataStoreService")

local DailyRewardDataStore = DataStoreService:GetDataStore("DailyReward")

Name your DataStore however you want, it doesn’t matter.
WARNING
If you change your name of the DataStore, the data you have already saved earlier will not be moved to the new one, however it is always possible to rename your DataStore to the old name and “recover” your old data.

Step 2.
Now we need to save the players login time every time they log in. We do that with the “PlayerAdded” event (fires when a players enters the game).

game.Players.PlayerAdded:Connect(function(player)
    local foundData = DailyRewardDataStore:GetAsync(player.UserId)
    if foundData then -- Player logged in before.
        DailyRewardDataStore:SetAsync(player.UserId,os.time())
    else -- Player has never entered the game.
        DailyRewardDataStore:SetAsync(player.UserId,os.time())
    end
end)

Step 3.
We now save the login time every time someone logs in, but we do not give them their reward (if they “deserve” it), so our next step is to detect if the existing login time is more than 24 hours in the past.

if tonumber(foundData) < os.time() - 86400 then -- (86400 seconds = 24 hours)
    print(player.Name.." is receiving their daily reward") -- Not necessary!

    -- Do your daily reward script!

    DailyRewardDataStore:SetAsync(player.UserId,os.time())
end

Step 4.
We are almost done! If you are sure, that your script is running without any errors you should add a pcall around the function (we do this to prevent our script from erroring if we failed to access the DataStore).
Our final code looks like this:

local DataStoreService = game:GetService("DataStoreService")

local DailyRewardDataStore = DataStoreService:GetDataStore("DailyReward")


game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local foundData = DailyRewardDataStore:GetAsync(player.UserId)
		if foundData then
			if tonumber(foundData) < os.time() - 86400 then
				print(player.Name.." is receiving their daily reward")

				-- Do your daily reward script!

				DailyRewardDataStore:SetAsync(player.UserId,os.time())
			end
		else
			DailyRewardDataStore:SetAsync(player.UserId,os.time())
		end
	end)
end)

Thank you for going through this tutorial! Do not forget to like this post if I helped you and write a comment if you have any questions or thanks!

62 Likes

os.date will probably be helpful, you can get the day, then check what day was the last time the player checked in, if it’s the same one, then don’t give the reward, otherwise, give the reward and set the async to the new day.

8 Likes

Just a typo :grinning_face_with_smiling_eyes: SetAsync instead of GetAsync

very cool, now I know how.

16 Likes

I wouldn’t recommend that since the player could just redeem their reward right before midnight and redeem it again 5 minutes later. If you check the hour its 100% sure that 24 hours have passed.

8 Likes

I think it makes life easier for the player so they don’t have to memorize what time they joined your game yesterday, and as long as they are rejoining and you are getting that engagement and player retention, I don’t think it would be a problem.

6 Likes

Hey, where will I place this script? And is this going to be a local script?