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

How do I make a Data Store with a Daily Reward?

Asked by 5 years ago
Edited 5 years ago

I have a game that has a daily reward in it, but I also made it so people can buy cash. Whenever someone buys cash, the daily reward breaks and they wont get the 5 cash daily reward.

Here's the daily reward script:

local datastoreService=game:GetService("DataStoreService")
local datastore=datastoreService:GetDataStore("ClubData")

local dailyRewardWait=24
local reward=5

game.Players.PlayerAdded:connect(function(p)
    local leaderstats=Instance.new("Folder")
    leaderstats.Name="stats"
    leaderstats.Parent=p
    local coins=Instance.new("IntValue")
    coins.Name="Coins"
    coins.Parent=leaderstats

    local userdata
    pcall(function()
        userdata=datastore:GetAsync(p.UserId)
    end)
    if userdata then
        if (os.time()-userdata.rewardTime)/3600>=dailyRewardWait then
            userdata={coins=userdata.coins+reward; rewardTime=os.time();}
            datastore:SetAsync(p.UserId,userdata)
        end
        coins.Value=userdata.coins
    else
        datastore:SetAsync(p.UserId,{
            coins=0;
            rewardTime=os.time();
        })
    end
end)

Here's the data store script:

CashStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

function PlayerEntered(player)
    repeat wait() until player.Character

    local cash = player.stats.Coins

    if CashStore:GetAsync("Points_"..player.Name) ~= nil then
        cash.Value = CashStore:GetAsync("Points_"..player.Name)
    else
        cash.Value = 0
    end

    cash.Changed:connect(function(Val)
        CashStore:SetAsync("Points_"..player.Name, Val)
    end)
end

game.Players.PlayerAdded:connect(PlayerEntered)

Answer this question