Want to stay connected to the Scripting Helpers community and get real-time help? Join the Scripting Helpers Discord Server!
Ad
Log in to vote
0

Why datastore does not save the value?

Asked by 1 year ago

I'm sorry, I use Google translator!

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")



game.Players.PlayerAdded:connect(function(player)
    local playerKey = "user_" .. player.userId
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local Wins = Instance.new("IntValue")
    Wins.Name = "Wins"
    Wins.Value = DataStore:GetAsync(playerKey)
    Wins.Parent = leaderstats
    print(playerKey)
    while wait(5) do 
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player:FindFirstChild("leaderstats") then
            player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
            DataStore:SetAsync(playerKey, Wins)

        end
    end
end
end)


if you remove the "DataStore: GetAsync (playKey)", the leaderboard will show Wins, but will not save

But if you leave this line, it will not save, as well as leaderboard will not show Wins

0
You're saving the last created Wins instance, not the Value of the current player's Wins. This is all wrong. RubenKan 2914 — 1y

1 answer

Log in to vote
0
Answered by
P100D 480
1 year ago
Edited 1 year ago

You're sending the actual IntValue to the datastore. Line 19 should read:

DataStore:SetAsync(playerKey, Wins)

Are you looking at the output window? It should give you some sort of error about writing the wrong type of data to an IntValue.

0
15:06:42.299 - Workspace.Save/Load Options:12: attempt to index upvalue 'DataStore' (a nil value) , Yes, your answer is correct, but it is not the solution to my problem :( TheAdmiralMaxwell 60 — 1y
Ad

Answer this question