Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to save time left of daily reward?

Asked by 4 years ago

Hello all!

im play in lobby, got reward, give only 3 hours, rejoin game again got reward after closed game, what is save time no working :/ No know how to save the time left after closing, can fix? :3 - (when close game,to need time saved)

wait(10800) -- it 3 hours = 10800 seconds

-

currency = "Money"  
moneyboost = 25000              
debounce = false        



    function onTouch(hit)
        if hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == false then
            if game.Players:findFirstChild(hit.Parent.Name) ~= nil then
            local ThisPlayer = game.Players:findFirstChild(hit.Parent.Name)
            ThisPlayer.leaderstats:findFirstChild(currency).Value = ThisPlayer.leaderstats:findFirstChild(currency).Value + moneyboost
            if ThisPlayer.leaderstats:findFirstChild(currency).Value then
            game.Workspace.DailyReward.Parts.Gui.CustomPlayerTag.PlayerName.Text = "You were awarded!"
            end
            debounce = true
            wait(10800)     
            game.Workspace.DailyReward.Parts.Gui.CustomPlayerTag.PlayerName.Text = "Daily Reward"
            debounce = false
            end
        end
    end


script.Parent.Touched:connect(onTouch)




3 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

This utilizes DataStores. A Service that allows the developer to create or and or access a database tied to their game. Simultaneously using the tick() function, we can use timestamps. Pairing it with this Service to perform mathematical equations, we can detect the time passing.

local DataStoreService = game:GetService("DataStoreService")
local DailyRewardTime = DataStoreService:GetDataStore("DailyReward")
local PreviousTimeStamped

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
    local TimeRetrieve = pcall(function()
        PreviousTimeStamped = DailyRewardTime:GetAsync(Player.UserId)
    end)
    if (TimeRetrieve and PreviousTimeStamped) then
        if ((tick() - PreviousTimeStamped/3600) >= 24) then
            --// Add Coins
            DailyRewardTime:SetAsync(Player.UserId, tick()) --// Reset
        end
    end
end)

Players.PlayerRemoving:Connect(function(Player)
    DailyRewardTime:SetAsync(Player.UserId, tick()) 
end)
0
nice answer, but consider not using anonymous values in your code, it's for code maintainability, and helps someone predict what the code does before even reading it.. but great answer :) User#23252 26 — 4y
0
Uh? This doesn't matter... at all, in fact, most representation of DataStore codes use undeclared variables. This is advanced programming. Ziffixture 6913 — 4y
0
i mean, when someone sees `PreviousTimeStamped/3600` it's not immediately clear of what this is for, any professional programmer will tell you not to use anonymous values User#23252 26 — 4y
0
for simple applications, its fine, but for production, its not. trust me, i've been in situations where there's many anonymous values to quickly comprehend the code User#23252 26 — 4y
0
Well, there is only one, so don't be so pressed about it Ziffixture 6913 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

As the other answer mentioned, you do, indeed, need to make a DataStore.

A quick crash-course on how to create a DataStore:

local datastoreService = game:GetService("DataStoreService");
local datastore = datastoreService:GetDatastore("DataStoreName"); -- You can save stuff to whatever DataStore you need.
--[[
You can save player's information to a Datastore VIA datastore:SetAsync(key, value);
key is what you have saved (so like the player's UserID) and value is what you want to save to that, so think of it like a dictionary.

You can get a player's saved data VIA datastore:GetAsync(key); -- retrieves all the information saved to 'key'.
]]--

If I didn't explain it well enough, here's some resources that I think you will benefit from.

[ What are DataStores? ] https://developer.roblox.com/en-us/articles/Data-store

[ Tutorial ] https://m.youtube.com/watch?v=DkYupSBUpes

Log in to vote
0
Answered by 4 years ago

You need to create a datastore, I'm not 100% sure on how to do this but you can look up a video on datastore.

Answer this question