Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

4 comments

0

Uh im not familiar with saving data with Tables, so this didn't really help

0

I edited the answer. If you want to reduce requests, and also save your data, then use an array/table.

0

I don't use dictionaries, only tables. That's cuz I'm cool.

0

Okay maybe I use dictionaries sometimes :p

This discussion is preserved as it was. New answers, comments, and votes are closed.