Daily Reward System

I have been focusing time scripts in roblox and here is my new released Daily Reward system! You obviously change the script to change what you want to do if it is the day after, thanks!

local Players = game:GetService("Players")
local dataService = game:GetService("DataStoreService")
local timeData = dataService:GetDataStore("lastLogin")
local dayDifference = 1

Players.PlayerAdded:Connect(function(Player)
local timeKey = (Player.UserId..'-$loginTime')
local startingDate
local claimedToday = Instance.new("BoolValue")
claimedToday.Parent = Player
claimedToday.Name = "claimedToday"
local success, err = pcall(function()
startingDate = timeData:GetAsync(timeKey)
end)
if success then
   if startingDate then
      local startingString = string.split(startingDate,' ')
      local todayDate = os.date()
      local splitString = string.split(todayDate,' ')
      if tonumber(splitString[3]) == tonumber(startingString[3] + dayDifference) and splitString[2] == startingString[2] and splitString[1] ~= startingString[1] then
         print('Day after!')
         claimedToday.Value = true
      elseif splitString[1] == startingString[1] then
         print('Same Day - '..os.date("%A"))
      else
         print(tonumber(splitString[3] - startingString[3]), 'days missed!')
      end
   else
      print('No past date!')
   end
else
   print(err)
end
end)

Players.PlayerRemoving:Connect(function(Player)
if Player:WaitForChild("claimedToday").Value == true then
   local timeKey = (Player.UserId..'-$loginTime')
   local success, err = pcall(function()
   timeData:SetAsync(timeKey,os.date())
   end)
   if success then
      print(Player.Name..' left and data saved successfully!')
   else
      error(err)
   end
end
end)

The way this works is that when the player leaves it saves the date they left and when they rejoin it gets the current date and compares the two to check if they join either the amount of days added in dayDifference after, the same day or over the dayDifference you added. The dayDifference is the amount of days you want the reward to fall under, 1 is daily, obviously. You can add your own daily benefits by editing where it says “print(‘Day after!’)”. This is the part that shows the day after, where it prints days missed it shows the amount of days since the player joined if it wasn’t the day after and for the Same Day, well… it explains itself! If there are any other issues feel free to reply to the forum or DM my Discord: aaronmansfield5#6864

21 Likes

This article is great but you should be more descriptive in how it works, and how to use it for your game.

2 Likes

I will edit the description now!

I edited it just now! Thanks for the suggestion.

1 Like

Great script, will be using it. Maybe add a ui were you click daily for your reward, like in most games.

Why not just get the last collected time then subtract that by the time for a day.

this is easier as it gives you the day

that’s for others to do, I just wrote the script!

Since it gets the time when you last joined, what if I was an hour away from getting my daily reward and I joined early but then left. Would it save my time and not let me get my daily reward in an hour? I think it would be better to save the time when the reward was claimed, not when the player joins.

I’d honestly just store the dates using UNIX. It’s a whole lot easier to do.

2 Likes

Ah yeah, never thought of that, I’ll fix it now!

I have just been working on something else rn, so I will get around to it.

This is basically what my suggestion was.

Yeah I know, I was just elaborating your claim.

How would I go about making a countdown until you can next claim with this script?