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

How do you save multiple values with DataStore?

Hello! My question is how do you save multiple values using DataStore? I already have a DataStore script. It used to save one value. I tried to make it save 2 recently. I tried so many times and the DataStore script just becomes worse.The first value doesn't even save anymore. Thanks for helping.

script:

game.Players.PlayerAdded:Connect(function(plr)
	
	local h = Instance.new("Folder",plr)
	
	h.Name = "leaderstats"
	
	local n = Instance.new("NumberValue",h)
	
	n.Name = "Trophies"
	
	n.Value = 0
	
	local m = Instance.new("NumberValue",h)
	
	m.Name = "Stage"
	
	m.Value = 1
	
	local i = Instance.new("NumberValue",plr)
	
	i.Name = "Coins"
	
	i.Value = 100

	local data
	
	local data2
	
	local success, errormessage = pcall(function()
		
		data = DataStore:GetAsync(plr.UserId.."-Trophies")
		
	end)
	
	if success then
		
		n.Value = data
		
	else
		
		print("error")
		
		warn(errormessage)
		
	end
	
do
	
	local success, errormessage = pcall(function()
		
		data2 = DataStore:GetAsync(plr.UserId.."-Coins")
		
	end)
	
	if success then
		
		n.Value = data2
		
	else
		
		print("error")
		
		warn(errormessage)
		
		end
			
	end

end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, errormessage = pcall(function()
		
		DataStore:SetAsync(plr.UserId.."-Trophies",plr.leaderstats.Trophies.Value)
		
	end)
	
	if success then
		
		print"success"
		
	else
		
		print"error"
		
		warn(errormessage)
		
	end
	
do
	
	local success, errormessage = pcall(function()
		
		DataStore:SetAsync(plr.UserId.."-Coins",plr.Coins.Value)
		
	end)
	
	if success then
		
		print"success"
		
	else
		
		print"error"
		
		warn(errormessage)
		
		end
		
	end
	
end)

5 comments

0

need your code

0

ok wait a minute

0

there

0

i made a video on how to save an inventory

0

Please remember to format your code before pasting it here

1 answer

✓ Accepted answer

Hi! In the first :GetAsync(), you need to sync both values to an existing data. To do so, you can do the following:

n.Value = Data.Trophies
m.Value = Data.Stage
i.Value = Data.Coins

In total, your :GetAsync() code should look like this:

local Data = DataStore:GetAsync(Player.UserId)

if Data then

	n.Value = Data.Trophies
	m.Value = Data.Stage
	i.Value = Data.Coins

end)

Now, in order to save it, you'll have to use the :SetAsync() on all existing DataStore values. To do so, you can do the following:

DataStore:SetAsync(Player.UserId, {

	["Trophies"] = Player.leaderstats.Trophies.Value;
	["Stage"] = Player.leaderstats.Stage.Value;
	["Coins"] = Player.leaderstats.Coins.Value;

})

Now the :SetAsync() code should look like this:

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

DataStore:SetAsync(Player.UserId, {

	["Trophies"] = Player.leaderstats.Trophies.Value;
	["Stage"] = Player.leaderstats.Stage.Value;
	["Coins"] = Player.leaderstats.Coins.Value;

})

end

Hope this helps!

1 comment

0

Thank you very much!

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