Daily Reward Module

Hello to everyone,
I want to give you know my new module, used to Daily rewards.
It has 2 easy and simple functions: Module.AdwardRewardIfCanAndSetNewTime() and module.ClearPlayerInformation().
The function Module.AdwardRewardIfCanAndSetNewTime() is used to chek if the time since the last user reward is more or equal to the amount of hours setted in the line 11 of the module. If it is bigger it sets the new data. To adward the reward, just use that function once and it will return if the user is able to get the reward or not as the bools true or false.
The customizations are also simple: If you want the data of the module such as the time since the last reward, etc and if you want the module to print the erros while using.
I hope you to use this module and to learn of it, have a nice day!

Code example:

local Module = require(script["DailyReward Module"])
local players = game:GetService("Players")
local RewardAmount = 10

players.PlayerAdded:Connect(function(player)
   local CanGetReward = Module.AdwardRewardIfCanAndSetNewTime(player)
   if CanGetReward then
      player:WaitForChild("leaderstats"):WaitForChild("Money").Value += RewardAmount
   end
end)

Module Link
Note: This module can just be used by the server and not by the client (in normal scripts).

Updates:

  • 03/7/2023 (Current version): Now, the module function module.AdwardRewardIfCanAndSetNewTime() return in second position the number of hours remaining for the next price. Example of the code:
local Module = require(script["DailyReward Module"])
local players = game:GetService("Players")
local RewardAmount = 10

players.PlayerAdded:Connect(function(player)
   local CanGetReward, TimeToNextReward = Module.AdwardRewardIfCanAndSetNewTime(player)
   if CanGetReward then
      player:WaitForChild("leaderstats"):WaitForChild("Money").Value += RewardAmount
   else
      print("Player "..player.name.." needs to wait "..TimeToNextReward.." for the next reward.")
   end
end)

This is usefull to add a countdown to say the user how much time do need to wait.
Thanks to @lilmazen1234 for the idea of the time.

14 Likes

Can we get screenshots/videos of what this looks like?

2 Likes

This module prob basicly save last time player checked & inputed into datastore. Then every time player joins it check if it >= 1 day then return as boolean value.
(Maybe?)

1 Like

Hi, just wondering why this is being released? Even on DevForum, you can find many daily reward tutorials online.

2 Likes

No, with the main function you just check if the 24 passed. If yes it return true and update the last reward to that moment. Else it return false

Because I was bored and I wasn’t finding a recent one, just boring.

Wym 24 its the same like what i said 1 day right

No, in the module settings you can set the hours you want between rewards. The deatfull is 24.

No dot should be there.

.

2 Likes

Thanks, I forgot
I writed the code directly in the devforum, sorry. :shushing_face:

All good, just informing you. .

1 Like

Really good start for a Daily Reward system, but maybe have a number for consecutive days get returned, then this would be something I would use! Personally, I think it would be beneficial and would get more people to use this module.

2 Likes

It doesn’t work for me. I had a daily reward system which I tried to use earlier but it didn’t work. So I replaced it with this and it’s still not working. I managed to fix it though by just checking if the time data was less than os.time() - 86400

Can you send more information about your issue?

I fixed it myself. You’re module literally doesn’t work it divides os.time()/86400 which doesn’t work for 24h since it returns 19,000 instead.

I don’t know what do you mean, my module literally don’t have do that. It don’t have even an “8”:

local PrintData = false --If you want the data to be printed in the output
local PrintError = true --If you want the errors to be printed in the ourput.
local Hours = 24 --Number of min hours btween rewards

local DataSS = game:GetService("DataStoreService")
local Storage = DataSS:GetDataStore("DailyRewardStorage")

local module = {}

function module.AdwardRewardIfCanAndSetNewTime(player:Player)
	if player and player:IsA("Player") then
		local CanGetReward = false
		Storage:UpdateAsync(player.UserId.."LastReward", function(LastValue)
			if LastValue then
				local HoursSince = (os.time()-LastValue)/3600
				if HoursSince >= Hours then
					CanGetReward = true
					if PrintData then
						print("Hours since last reward: "..HoursSince..", can get the reward.")
					end
					return os.time()
				else
					if PrintData then
						print("Hours since last reward: "..HoursSince..", can not get the reward.")
					end
					return LastValue
				end
			else
				if PrintData then
					print("New user, can get reward")
				end
				CanGetReward = true
				return os.time()
			end
		end)
		return CanGetReward
	else
		if PrintError then
			error("No player given as parameter 1.")
		end
	end
end

function module.ClearPlayerInformation(player:Player)
	if player and player:IsA("Player") then
		Storage:RemoveAsync(player.UserId.."LastReward")
	else
		if PrintError then
			error("No player given as parameter 1.")
		end
	end
end

return module

As you can see, if you search with Control+F you won’t find “os.time()/86400”
image
Instead, it gets the diference between the both amounts of seconds and /3600 (to get the hours amount). Can you send me the error your got (if it was printing) or another explication please?

That’s besides the point it’s diving os.time() so the amount of seconds since 1970 by 3600 which is almost always over 24 so it game me the daily reward every time i joined my game.

I don’t understand you, my code divide the diference btween the amount of sec since 1970 and the actual date as you can see here:

And then it compare it to the amount of hours the user decide here:

It won’t always get 24 hours.
Maybe im undertstanding bad since im not english and im not the better in the world talking it.

Yeah it doesn’t always get 24 hours because it always gets above 24 hours and gives me the daily reward everytime I join in studio.

Is it only set like that in studio so I can test it or does it straight up not work?