How would I save and get multiple values using datastore?

@FunAsiK said is true but it’s kinda pain.

Just loop all children inside folder and save the value

game.Players.PlayerRemoving:Connect(function(player)
	if player.Storage.OwnsServer.Value == true then
		local success, err = pcall(function()
			local tableToSave = {}

			for i, item in pairs(player.Storage:GetChildren()) do
	Tabletosave[tem.Name] = item.Value
	print(i)
end


			if player.Storage.OwnsServer.Value == true then
				serverStorage:SetAsync(player.UserId.."-ServerStorage", tableToSave)
			end
		end)

		if success then
			print("Successfully saved data")
		else
			warn(err)
		end
	else
		print(player.Name.." doesn't own a server!")
	end
end)

Sorry i can’t help much cuz im on phone

Try to change this:

player.Storage[i] = item

On this:

player.Storage[i].Value = item

And see if this works.

Nope. Still doesn’t get data.

charactera

I would say you need to utilize a dictionary instead of a simple array when trying to load and save specific data so when using GetAsync the script will understand which value is which.

Try this:

local datastoreservice = game:GetService("DataStoreService")
local serverStorage = datastoreservice:GetDataStore("ServerStorage")

game.Players.PlayerAdded:Connect(function(player)
	local storage = Instance.new("Folder")
	storage.Name = "Storage"
	storage.Parent = player

	local ownsServer = Instance.new("BoolValue")
	ownsServer.Name = "OwnsServer"
	ownsServer.Parent = storage

	local serverName = Instance.new("StringValue")
	serverName.Name = "ServerName"
	serverName.Parent = storage

	local owner = Instance.new("StringValue")
	owner.Parent = storage
	owner.Name = "Owner"

	local data

	local success, err = pcall(function()		
		data = serverStorage:GetAsync(player.UserId.."-ServerStorage")
	end)
	
	if (data) then
		print("Data successfully loaded")
		for itemName, itemValue in pairs(data) do
			player.Storage[itemName].Value = itemValue
			print(itemName, "set to", itemValue)
		end
	else
		print("No data found for the player")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	if player.Storage.OwnsServer.Value == true then
		local success, err = pcall(function()
			local tableToSave = {}
			
			tableToSave["OwnsServer"] = player.Storage.OwnsServer.Value
			tableToSave["ServerName"] = player.Storage.ServerName.Value
			tableToSave["Owner"] = player.Storage.Owner.Value

			if player.Storage.OwnsServer.Value == true then
				serverStorage:SetAsync(player.UserId.."-ServerStorage", tableToSave)
			end
		end)

		if success then
			print("Successfully saved data")
		else
			warn(err)
		end
	else
		print(player.Name.." doesn't own a server!")
	end
end)

Also, make sure to check if this conditional statement is true; it could technically not error, but still be false and not save data in the first place.

if player.Storage.OwnsServer.Value == true then
	serverStorage:SetAsync(player.UserId.."-ServerStorage", tableToSave)
end
1 Like

Issue fixed through a YouTube video. Thanks for everyones help!