I'm curious on how to use os.time() to make a daily gift script that would give rewards every 12 hours.
How can you make a daily gift script using os.time() and datastore?
1 answer
HI friend.
I will try to teach you how to make daily rewards,however i am not that good in explaining.So i will try my best today,Good Luck!
Making your values
So first of all,you would want to insert a script into your Workspace or ServerScriptService,(either of them works,haven't tried putting is in sss)
Then insert one NumberValue and one StringValue.
Name the number value to "Reward" and the StringValue to "Stats"*
*You have to create your leaderstats first.
Assuming your leaderboard is done,In the stringvalue,Put the name of your stats inside.Besure to type it correctly!
and your number value to the amount of _______ you want the player to get each day .
Script
So now what? Its time to code everything!
You would want to make the datastore so when the player joins,It only gets one perday.
So you might be wondering,What is the difference between tick and os.time()
Well,tick can be used but it isn't useful as the user can change the time and get the reward.While os.time,it only sticks to one timing unless you change account.
Then you would want to make your timeperiod. 12 hours is about 43200 mins or so
So now,This should be the code
game.Players.PlayerAdded:connect(function(plr)
script.DailyReward:Clone().Parent = plr.PlayerGui--Clone your guis into PlayerGui NOT StarterGui
end)
local DataStore = game:GetService('DataStoreService'):GetDataStore('Name of your DataStore')--Name your dailyreward system
local TimePeriod = 43200--your countdown
function Give(plr)
assert(coroutine.resume(coroutine.create(function()-- I forgot what this does tho but just leave it here incase.
if plr:FindFirstChild("leaderstats") then
plr.leaderstats[script.Stats.Value].Value = plr.leaderstats[script.Stats.Value].Value + script.IncreaseBy.Value--PAYDAY!
end
local Gui = plr:WaitForChild('PlayerGui')
if Gui:FindFirstChild('your guis name here') then
local as = Gui.DailyReward.Frame--Makes the guy which tells the player visible
as.Visible = true
end
end)))
end
game.Players.PlayerAdded:connect(function(plr)-- datastore stuff,you don't really want to mess here if you don't know what you are doing
local currenttime = os.time()
local RecordedPreviousTime = DataStore:GetAsync(plr.userId)
if RecordedPreviousTime then
if type(RecordedPreviousTime) == 'number' then
local timeelapsed = currenttime - RecordedPreviousTime
print(timeelapsed)
if timeelapsed >= TimePeriod then
DataStore:SetAsync(plr.userId, os.time())
warn(plr.Name .. ' (' .. plr.userId .. ') rewarded.')
Give(plr)
end
else
warn(plr.Name .. ' (' .. plr.userId .. ')\'s has been destoryed,trying to fix the problem!')
DataStore:SetAsync(plr.userId, os.time())
Give(plr)
end
else
warn(plr.Name .. ' (' .. plr.userId .. ')\'s has not been found. Creating...')
DataStore:SetAsync(plr.userId, os.time())--yep!
Give(plr)
end
end)
Make the guis to tell players that the received the award.
I assume you already know how to make guis Make sure you DO NOT place the guis into startergui. You would also want a text button so the player can close it
script.Parent.MouseButton1Down:connect(function()
script.Parent.Parent.Visible = false--Closes the frame or whatever
end)
Final notes
IF you would like to go further,You can make the rewards increase each time.
1 comment
if anything is wrong,let me know
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Have you tried to make it yet?