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

Why is my data store not working? It works for other games..

Asked by 4 years ago

Okay, so I scripted this data store and it works in my other games but doesn't for this game. I have checked this many times and it doesn't work, I'm very confused on why it doesn't save. Ignore the daily reward, what I'm focusing on is the data store.

Heres the script.

local serverStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")

local hourWait = 24

local possibleRewards = {50,50,50,50,50,50,50,50,50,70,63,63,63,63,63,70,70,70,90,90,100}

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

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Money = Instance.new("NumberValue")
    Money.Name = "Money"
    Money.Parent = leaderstats

    local timeNow = os.time()

    local data

    pcall(function()
        data = DataStore:GetAsync(player.UserId.."-dailyReward")
        print("Getting data")
    end)

    if data ~= nil then
        -- Returning player to the game.

        local timeSinceLastClaim = timeNow - data -- Number in sec since last reward was claimed

        print("Time since last claim"..timeSinceLastClaim)

        if (timeSinceLastClaim / 3600) >= hourWait then
            -- They are elgible for a reward! :D
            local reward = possibleRewards[math.random(1,#possibleRewards)]
            game.ReplicatedStorage.ShowDailyReward:FireClient(player,hourWait,reward)
            local connection
            connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
                if triggeringPlayer == player then
                    print("Reward Claimed!")
                    player.leaderstats.Money.Value = player.leaderstats.Money.Value + reward
                    DataStore:SetAsync(player.UserId.."-dailyReward",os.time())
                    connection:Disconnect()
                end
            end)
        else
            print("Player is uneligible right now. :(")

        end

    else
        -- New Player
        print("New player!")
        local reward = possibleRewards[math.random(1,#possibleRewards)]
        game.ReplicatedStorage.ShowDailyReward:FireClient(player,hourWait,reward)
        local connection
        connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
            if triggeringPlayer == player then
                print("Reward Claimed!")
                player.leaderstats.Money.Value = player.leaderstats.Money.Value + reward
                DataStore:SetAsync(player.UserId.."-dailyReward",os.time())
                connection:Disconnect()
           end
       end)
    end


    game.Players.PlayerRemoving:Connect(function(player)
        local data
        pcall(function()
            data = DataStore:GetAsync(player.UserId.."-dailyReward")
            print("Getting the Data... or is it?")
        end)
    end)

    --[[if data == nil then
        -- New player once again.
        pcall(function()
            local timeVal = os.time()
            DataStore:SetAsync(player.UserId.."-dailyReward")
        end)
        print("Saved because you are a new player")
    end--]]

    local dataFolder = Instance.new("Folder")
    dataFolder.Name = player.Name
    dataFolder.Parent = serverStorage.RemoteData

    local debounce = Instance.new("BoolValue")
    debounce.Name = "Debounce"
    debounce.Parent = dataFolder

    local MoneyData

    local success,errormessage = pcall(function()
        MoneyData = DataStore:GetAsync("Money-"..player.UserId)
    end)

    if success then
        print("data saved")
        if MoneyData then
            Money.Value = MoneyData
        end
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        DataStore:SetAsync("Money-"..player.UserId,player.leaderstats.Money.Value)
    end)
end)

0
Instead of `local leaderstats = Instance.new("Folder")` then `leaderstats.Parent = player`, just do `local leaderstats = Instance.new("Folder, player")` nc2r 117 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Alright so if it works in other games then you most likely forgot to add something, this is usually the case... I've done it multiple times, I've even over-looked it multiple times until I finally figure out what I forgot about a month later. Try seeing if there's something that you forgot to transfer to the new game.

1
usually the case. Make sure the game settings are the same(when necessary.) ForeverBrown 356 — 4y
0
i copied and pasted it from my game and just changed the stats and i have checked countless times dragonlolpp2 2 — 4y
0
i copied and pasted it from my game and just changed the stats and i have checked countless times dragonlolpp2 2 — 4y
1
WAIT NVM YOU ARE RIGHT I FORGOT TO ADD SOMETHING THANKS dragonlolpp2 2 — 4y
Ad

Answer this question