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.
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?)
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.
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
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”
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.