Datasave Compacting Help

Is it possible to remove data2 from the script? Only do StageDataStore:GetAsync(player.UserId) once?

local DataStoreService = game:GetService("DataStoreService")
local StageDataStore = DataStoreService:GetDataStore("StageSave")

game.Players.PlayerAdded:Connect(function(player)
	local data
	local data2
	local success, errorMessage = pcall(function()
		data = StageDataStore:GetAsync(player.UserId)
		data2 = StageDataStore:GetAsync(player.UserId)
	end)
	
	if success then
		player.leaderstats.Stage.Value = data
		player.leaderstats.Rebirths.Value = data2
	else
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data
	local data2
	local success, errorMessage = pcall(function()
		data = StageDataStore:SetAsync(player.UserId, player.leaderstats.Stage.Value)
		data2 = StageDataStore:GetAsync(player.UserId, player.leaderstats.Rebirths.Value)
	end)
	
	if success then
		print("Data saved")
	else
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	for i, v in pairs(game.Players:GetChildren()) do
		v:Kick("Server closed")
	end
	wait(2)
end)
1 Like

Yes, this requires using tables which I’ll quickly write and explain in a second:

Saving part:

data = StageDataStore:SetAsync(player.UserId, {player.leaderstats.Stage.Value, player.leaderstats.Rebirths.Value} )

Loading part:

player.leaderstats.Stage.Value = data[1]
player.leaderstats.Rebirths.Value = data[2]

By the way, I posted this first so it would be helpful if you set it as the solution =) Thanks!!

1 Like

Saving mutliple values with datastores


1 | We usually save tables into 1 datastore which is much more clean and more accessible for multiple purposes. In your script you’re trying to save Rebirths and Stages so we make our table like this:

local data = { -- The table
    Stage = player.leaderstats.Stage.Value, -- Stage value
    Rebirths = player.leaderstats.Rebirths.Value, -- Rebirth value
} -- Closes the table

2 | Then we just save the table instead of the values:

StageDataStore:SetAsync(player.UserId, data)

3 | To obtain the values you first want to get them like this:

local data -- this is nil at first
local success, errorMessage = pcall(function() 
	data = StageDataStore:GetAsync(player.UserId) -- Sets data as the table
end)

4 | Then to access each value just simply use this:

Stage.Value = data["Stage"] -- data["Stage"] replace Stage with whatever you value name is!

Rebirths.Value = data["Rebirths"] -- data["Stage"] replace Rebirth with whatever you value name is!

Entire script:


local DataStoreService = game:GetService("DataStoreService")
local StageDataStore = DataStoreService:GetDataStore("StageSave")

game.Players.PlayerAdded:Connect(function(player)
	local data
	local success, errorMessage = pcall(function()
		data = StageDataStore:GetAsync(player.UserId)
	end)

	if data then
		player.leaderstats.Stage.Value = data["Stage"]
		player.leaderstats.Rebirths.Value = data["Rebirths"]
	elseif not success then
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data = {Stage = player.leaderstats.Stage.Value, Rebirths = player.leaderstats.Rebirths.Value}

	local success, errorMessage = pcall(function()
		StageDataStore:SetAsync(player.UserId, data)
	end)

	if success then
		print("Data saved")
	else
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	for i, v in pairs(game.Players:GetChildren()) do
		v:Kick("Server closed")
	end
	wait(2)
end)
1 Like

Thanks so much for the explanation :+1:

1 Like

Fine I’ll give you the solution merit :neutral_face:

No problem, just keep in mind a lot of people have problems when it comes to datastore so keeping it compact also means you’ll have to organize it and understand it in case a bug does appear so you know where it is.

thxβ€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž