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

Why is the data returning nil?

Asked by 3 years ago

I am trying to save the time a player last joined for a daily reward system, however, it returns nil once returning to the game every time, why is that?

local DataStore = game:GetService("DataStoreService"):GetDataStore("Delay")
local hourWait = 24




game.Players.PlayerAdded:Connect(function(player)

    local Time = os.time()
    local data 
    pcall(function()
        data = DataStore:GetAsync(player.UserId.."-Delay")
        print("Checking data for Daily Reward")
    end)


    if data ~= nil then

print("DATA IS NOT NIL")
        local timeSinceLastPlayed = Time - data
        player:WaitForChild("TimeSinceLastJoin").Value = timeSinceLastPlayed
        print("time since last joined was"..timeSinceLastPlayed)

        print("Last reward claimed at "..timeSinceLastPlayed)
        if (timeSinceLastPlayed/3600) >= hourWait then


            -- give reward


            DataStore:SetAsync(player.UserId.."-Delay",os.time())
        end

    else

        print("no data")-- No data
    end


    game.Players.PlayerRemoving:Connect(function(player)
        local data

        pcall(function()
            data = DataStore:GetAsync(player.UserId.."-Delay")
            print("checking data")
               print("your data is " .. data)

        end)
        if data == nil then

            pcall(function()
                local timeValue = os.time()
                DataStore:SetAsync(player.UserId.."-Delay")

                -- data for new player saved
            end)
        end
    end)


end)

Answer this question