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

Why won't my DataStore load or save?

Asked by 5 years ago

It prints Data loaded and Data Saved when entering or leaving the game, but it doesn't actually save. When I join the game it still says 0.

local Datastore = game:GetService("DataStoreService"):GetDataStore('Rocky-Test91')

game.Players.PlayerAdded:Connect(function(player)
    local Key = 'Player-ID:' .. player.userId
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player

    local Pebbles = Instance.new("IntValue")
    Pebbles.Name = 'Pebbles'
    Pebbles.Value = 0
    Pebbles.Parent = leaderstats

    local GetSave = Datastore:GetAsync(Key)
    if GetSave then
        Pebbles.Value = GetSave
        print('Data Loaded for '..player.Name)
    else
        local Numbers = {Pebbles.Value}
        Datastore:SetAsync(Key, Numbers)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Key = 'PlayerID:' .. player.userId
    local ValueToSave = {player.leaderstats.Pebbles.Value}
    Datastore:SetAsync(Key, ValueToSave)
    print('Data Saved For '..player.Name)
end)
0
Instead of "local Key = 'PlayerID:' .. player.userId" just do "local Key = player.userId" im_Draco 3 — 5y
0
That won't work. Datastores only accept strings as keys. One thing to note: It's "player.UserId" not "player.userId." User#25115 0 — 5y

1 answer

Log in to vote
0
Answered by
Sorukan 240 Moderation Voter
5 years ago
Edited 5 years ago

You set ValueToSave as a table, i'm not sure why but since it's a table all you're doing to calling the table itself and not the values inside it. Also it's good practice to use a BindToClose function at the end so the data will be saved when the server is about to shut down.

local Datastore = game:GetService("DataStoreService"):GetDataStore('Rocky-Test91')

game.Players.PlayerAdded:Connect(function(player)
    local Key = 'Player-ID:' .. player.userId
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player

    local Pebbles = Instance.new("IntValue")
    Pebbles.Name = 'Pebbles'
    Pebbles.Value = 0
    Pebbles.Parent = leaderstats

    local GetSave = Datastore:GetAsync(Key)
    if GetSave then
        Pebbles.Value = GetSave
        print('Data Loaded for '..player.Name)
    else
        local Numbers = {Pebbles.Value}
        Datastore:SetAsync(Key, Numbers)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Key = 'PlayerID:' .. player.userId
    local ValueToSave = {player.leaderstats.Pebbles.Value}
    Datastore:SetAsync(Key, ValueToSave[1]) -- calls the value from the table
    print('Data Saved For '..player.Name)
end)

    game:BindToClose(function()
        for i,v in pairs(game.Players:GetPlayers()) do
            if v then
            v:Kick() -- kicks all players before server shuts down so their data will save from the PlayerRemoving event
        end
    end
    wait(3) -- waits 3 seconds before the server shuts down so the player's data will save
end)
0
Thanks, it worked. DuckOffender 6 — 5y
Ad

Answer this question