Daily Chest Timer

I have a script that scripts a daily chest, how do I make the timer stay true even after the player rejoins the game??

local Amount = math.random(250,500)
local CanGet = true
local Cd = 1
local Timer = 86400

local function onTouch(otherPart)
           local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
           if humanoid then
                   local player = game.Players:FindFirstChild(otherPart.Parent.Name)
                   if player and CanGet then
                          CanGet = false
                          player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + Amount
                          script.Parent.Part.Thing.Timer.Text = "Come Back In 24 Hours To Claim Your Reward!"
                          script.Parent.explosion.Enabled = true
                          wait(Cd)
                          script.Parent.explosion.Enabled = false
                          wait(Timer)
                          script.Parent.Part.Thing.Timer.Text = "Claim"
                          CanGet = true
                  end
         end
end

part.Touched:Connect(onTouch)```

You should try using os for this kind of stuff, everytime the player claims its reward just add 86400 seconds to the goal.

If you don’t understand os functions too well there are a lot of resources on the devforum and YouTube.

2 Likes

This should be handled on the server. What you can do is have a database of players and the time they joined last and update this whenever they join a server. Using that table, you can simply do a tick() call to get the number of seconds since epoch. Simple math later and you can determine if the last join was longer than a day ago :slight_smile:

thanks for linking mine! you’re a legend:)

1 Like

I took about an hour looking at os, but I still don’t understand, how would I incorporate it into the script?

use my system he linked if you like, but os.time() or tick()

Os is a base Lua library that includes a range of useful functions.

For your use you want to look specifically at os.clock or os.time

In your scenario I would opt for os.time, but they work similar (In this context). Simply, os.time returns the number of seconds that have passed since (1 January 1970, 00:00:00) UTC.

You can use this in cooperation with DataStoreService, such that when a player joins, you can load their data, and check if they have a timestamp saved to their data. If they do, you can compare it to the current time:

os.time() - savedtime

If this value > 86400 (seconds), then you know a day has passed. You can then save the new time value.

Sorry I am a bit late to the train on this, but as others have said this is well documented, and you should be able to find numerous resources:

1 Like