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

Why I can't save 2 Values in the Data Store?

Asked by
Borrahh 265 Moderation Voter
4 years ago
Edited 4 years ago

Basically, I'm trying to save 2 Datas, but it's not working, For Points, it works, data is saved. But for the Wins, it doesn't why?

local DATASTORE = game:GetService("DataStoreService"):GetDataStore("DATASTORE")
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local wins = Instance.new("IntValue")
    wins.Name = "wins"
    wins.Parent = leaderstats

    local Points = Instance.new("IntValue")
    Points.Name = "Points"
    Points.Parent = player

    local data
    local winsData
    local dataSaved, notSaved = pcall(function()
        data = DATASTORE:GetAsync(player.UserId.."-Points")
        winsData = DATASTORE:GetAsync(player.UserId.."-Wins")
    end)

    if dataSaved then
        Points.Value = data
        wins.Value = winsData
        print("Data Loaded")
    else
        print("Data not saved")
        warn(notSaved)
    end
end)

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

    local dataSaved, notSaved = pcall(function()
        DATASTORE:SetAsync(player.UserId.."-Points", player.Points.Value)
        DATASTORE:SetAsync(player.UserId.."-Wins", player.leaderstats.Wins.Value)
    end)

    if dataSaved then
        print("Data Saved")
    else
        print("Data not saved")
    end
end)

1 answer

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

Instead of using 2 SetAsync and 2 GetAsync commands, put the variables in a table, like this:

local dataTable = {
    Points = Points.Value
    Wins = Wins.Value
}

and use only 1 SetAsync and 1 GetAsync. The problem is that you're gonna deal with too many requests if the players join and you're setting 2 values, instead of 1. When you GetAsync, you can set the values from there, using this:

local success, err = pcall(function()
    DATASTORE:GetAsync(dataTable)
end)
if success then
    Points.Value = dataTable.Points
    Wins.Value = dataTable.Wins
end
0
Uh im not familiar with saving data with Tables, so this didn't really help Borrahh 265 — 4y
0
I edited the answer. If you want to reduce requests, and also save your data, then use an array/table. Dovydas1118 1495 — 4y
0
I don't use dictionaries, only tables. That's cuz I'm cool. Heavenlyblobmaster 271 — 4y
0
Okay maybe I use dictionaries sometimes :p Heavenlyblobmaster 271 — 4y
Ad

Answer this question