Hi! I started to make a timed chest for my simulator game but I do not know what to do next or how I should do it in the most effective way. I am trying to make a chest that gives rewards every 24 hours and it also updates a gui to show how long you have got left. Here is a script in the chest.
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("ChestDataStore1")
local coolDown = 24 -- Hours until chest can be opened
local function giveCrowns(plr)
local amount = 100
plr.leaderstats.Crowns.Value += amount
end
local function updateGui(num)
while wait(1) do
num -= 1
--UPDATE GUI
end
end
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if game.Players:GetPlayerFromCharacter(hit.Parent) then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
local lastTimeOpened = DS:GetAsync("plr_".. plr.UserId) or nil -- previousTime or nil
if lastTimeOpened then
local difference = os.time() - lastTimeOpened
if difference > coolDown * (60 * 60) then
DS:SetAsync("plr_".. plr.UserId, os.time())
giveCrowns()
updateGui(coolDown * (60 * 60))
end
else
DS:SetAsync("plr_".. plr.UserId, os.time())
giveCrowns()
updateGui(coolDown * (60 * 60))
end
end
end
end)