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

How can I make my Daily Reward timer after claiming the reward start again at 24 hours?

Asked by 4 years ago

Hello basiclly what I do like to ask is in the title. I already tried a solution with datastore cause it takes everytime the datastore but due to queue this won't work and I don't have a idea.

Claim script:

local DataRow = game.ReplicatedStorage.GetDataRow:InvokeServer()
local sec = 0
local hours = 0
local mins = 0


function Calculate()

    local data = game.ReplicatedStorage.GetData:InvokeServer()
    if data == 0 then
    data = os.time()
    data = data - 86500
end
    local TimeNow = os.time()
    print("working")
    local timesincelastclaim = TimeNow - data
    if timesincelastclaim >= 60 then
        repeat
        timesincelastclaim = timesincelastclaim -60
        mins = mins +1
        print(mins)
    until timesincelastclaim < 60
    end
    if mins >= 60 then
        repeat
        mins = mins -60
        hours = hours +1
    until mins < 60
    end
    sec = timesincelastclaim
    if sec == 60 then
        sec = 00
    end
    print(hours)
end

if DataRow == "" or DataRow == nil or DataRow == 0 then
    DataRow = 1
end

Calculate()
if hours >= 48 then
    DataRow = 1
end

Calculate()
if hours >= 48 then
    DataRow = 1
end

script.Parent.TextButton.MouseButton1Click:Connect(function()
    if DataRow == "" or DataRow == nil or DataRow == 0 then
    DataRow = 1
end
    print("pressed")
    Calculate()
    print("pressed")
    if hours >= 24 then
        game.StarterGui:SetCore("SendNotification", {
        Title = "Daily Reward",
        Text = "You claimed your daily reward!",
        Image = "rbxassetid://3593135345"
        })
        local saveofdatarow = DataRow
        DataRow = DataRow * saveofdatarow
        local MoneyToAdd = 100 * DataRow
        DataRow = saveofdatarow +1
        game.ReplicatedStorage.SetDataRow:InvokeServer()
        script.Parent.Plus.Text = "+ "..MoneyToAdd.." Money"
        script.Parent.TreasureChestOpen.Visible = true
        script.Parent.TreasureChest.Visible = false
        repeat 
            wait(0.08)
            script.Parent.Plus.TextTransparency = script.Parent.Plus.TextTransparency - 0.1
        until script.Parent.Plus.TextTransparency < 0.1
        wait(5)
        script.Parent.TreasureChestOpen.Visible = false
        script.Parent.TreasureChest.Visible = true
        repeat 
            wait(0.08)
            script.Parent.Plus.TextTransparency = script.Parent.Plus.TextTransparency + 0.1
        until script.Parent.Plus.TextTransparency > 0.9
        game.ReplicatedStorage.SetData:InvokeServer(time.os())
    else
        game.StarterGui:SetCore("SendNotification", {
        Title = "Daily Reward",
        Text = "You can't claim it now!",
        Image = "rbxassetid://3592767549"
        })
    end
end)

That's the text script:


local Hours = 23 local Mins = 60 local Secs = 59 local sec = 0 local hours = 0 local mins = 0 function Calculate() local data = game.ReplicatedStorage.GetData:InvokeServer() if data == 0 then data = os.time() data = data - 86500 end local TimeNow = os.time() print("working") local timesincelastclaim = TimeNow - data if timesincelastclaim >= 60 then repeat timesincelastclaim = timesincelastclaim -60 mins = mins +1 print(mins) until timesincelastclaim < 60 end if mins >= 60 then repeat mins = mins -60 hours = hours +1 until mins < 60 end sec = timesincelastclaim if sec == 60 then sec = 00 end print(hours) end Calculate() while wait(1) do if hours >= 24 then script.Parent.Text = "Available to claim! 00:00:00 time left!" else Hours = Hours - hours print(mins) Mins = Mins -mins Secs = Secs - sec if Secs == 60 then Secs = 00 end script.Parent.Text = "Available to claim in "..Hours..":"..Mins..":"..Secs Hours = 23 Mins = 60 Secs = 60 hours = 0 mins = 0 sec = 0 Calculate() end end

And the data store script:

local event1 = game.ReplicatedStorage.GetData
local event2 = game.ReplicatedStorage.GetDataRow
local event3 = game.ReplicatedStorage.SetData
local event4 = game.ReplicatedStorage.SetDataRow
local data1 = game:GetService("DataStoreService"):GetDataStore("Data1")
local data2 = game:GetService("DataStoreService"):GetDataStore("Data2")

event1.OnServerInvoke = function(player)
    local id = player.UserId
    local datasave = data1:GetAsync(id) or 0
    print(datasave)
    return datasave
end

event2.OnServerInvoke = function(player)
    local id = player.UserId
    local datasave = data2:GetAsync(id) or 0
    return datasave
end

event3.OnServerEvent:Connect(function(player, val)
    print(val)
    local id = player.UserId
    data1:SetAsync(id, val)
end)

event4.OnServerEvent:Connect(function(player, val)
    print(val)
    local id = player.UserId
    data2:SetAsync(id, val)
end)

Thanks for ideas/solutions.

0
dude whatever ur doing just save the tick and subtract it by the tick when the player gets back then if its equal to the amount of time spent, then reward the player stuff... greatneil80 2647 — 4y
0
Also if it doesnt work cuz of the que, dont save data too often, save it when the player is being removed greatneil80 2647 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You could use data store, like this; NOTE: I assume you already made a leaderstats of cash, but this will work on anything rewards:

in server script:

local dataName =  "DailyRewards_InThisCoolGame"
local datastore = game:GetService("DataStoreService"):GetDatastore(dataName)
local key_prefix = "DailyReward"
local newPlayerCash = 2500
local dailyReward = 250
local secondsInADay = 60*60*24 -- 60 seconds times 60 minute times 24 hours!

function getData (player)
    local key = key_prefix..player.UserId
    local lastTime,cash
    suc ,err = pcall(function()
        lastTime= datastore:GetAsync(key)
    end)

    if err  then
        print("Error")
    elseif suc then
        print("Success")
        if lastTime then
            print"not new player"
            local timeElapsed= os.time() - lastTime
            if timeElapsed>= secondsInADay then
                print"player can get reward"
                player.leaderstats.Cash.Value = player.leaderstats.Cash.Value+dailyReward
                print("player got daily reward")
            else
                print"player can't get reward"
            end
        else
            print"new player"
            player.leaderstats.Cash.Value = player.leaderstats.Cash.Value+newPlayerCash
        end
    end

end


function setData(player)
    local key = key_prefix..player.UserId
    suc ,err = pcall(function()
        datastore:SetAsync(key,os.time())
    end)

    if err then
        print("An error occurred")
    else
        print("Data saved successfully")
    end
end



game.Player.PlayerAdded:Connect(getData)


game.Players.PlayerRemoving:Connect(setData)


Ad
Log in to vote
0
Answered by 4 years ago

See if this helps:

YT Video

Answer this question