Multiple values in datastore

Evident by the title, I am by no means a good scripter. I need help adding more values to datastores. To make the datastore, I followed an older tutorial by AlvinBlox. In the tutorial, it doesn’t tell me how to add multiple datastores. Could someone please amend my code to work as intended?

Loading
players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local gemstones = Instance.new("IntValue")
	gemstones.Name = "Gemstones"
	gemstones.Parent = leaderstats
	
	local lvl = Instance.new("IntValue")
	lvl.Name = "lvl"
	lvl.Parent = leaderstats
	
	local xp = Instance.new("IntValue")
	xp.Name = "xp"
	xp.Parent = player
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-gemstones")
	end)
	
	if success then
		gemstones.Value = data
	else
		print("There was an error when getting data")
		warn(errormessage)
	end
end)
Saving
local function save(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-gemstones", player.leaderstats.Gemstones.Value)
	end)
	
	if success then
		print("Player data saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
	
end

I didn’t want to copy the function to add more (because I heard there is a limit to GetAsync)

In addition to the already saving “Gemstones”, I would like “xp” and “lvl” to save as well. (This should be obvious considering that it is already written in the code, but I just want to provide as much detail as possible to hopefully make it easier for anyone helping me. Thanks!

Edit: I also want to add that the tutorial instructed me to concacnate the userId to the name of the value intentionally to make it easier to add more values.

To do this instead of saving a singular value to a datastore as you normally would like this:

YOURDATASTORE:SetAsync(Key,SingleValue)

It would be best to instead save a dictionary, dictionaries can hold many values and can be saved to a datastore!

Like so!

local dataToSave = {
    Value1 = 0,
    Value2 = 15
}

-- when you want to save
YOURDATASTORE:SetAsync(Key,dataToSave)

-- when you want to load data
local data = YOURDATASTORE:GetAsync(key) -- returns dictionary
-- lets say we have a number value called Value1 and Value2, lets set their values
Value1.Value = data.Value1
Value2.Value = data.Value2

-- if you wanted to change the values in said dictionary you can do this:
data.Value1 = 20 -- replace 20 with whatever you want to set it to!

Overall dictionaries are really great tools and I’d recommend looking into them!
If you have any questions feel free to ask!

1 Like

Hi! Thanks for your prompt response! I found it really useful!

One issue that I ran into was when a player that has no saved data it gives this error:

Is there anything I should do to initially set their data if “data” doesn’t exit yet?

So when you get the data, check if its not nil like so:

if not data then
-- no data
end

if there isnt data then youll need to set the data to something like the dataToUse dictionary I showed earlier

2 Likes

make sure to also wrap requests in pcall or xpcall for error handling purposes

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.