How would I save and get multiple values using datastore?

I am trying to save multiple values IF one of the values is true. So for example, if the player has a server in the game, the OwnsServer value would be true and then the player’s data would be saved.

Here is what I have so far:

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()		
		return serverStorage:GetAsync(player.UserId.."-ServerStorage")
	end)
end)

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

			table.insert(tableToSave, player.Storage.OwnsServer.Value)
			table.insert(tableToSave, player.Storage.ServerName.Value)
			table.insert(tableToSave, player.Storage.Owner.Value)


			if player.Storage.OwnsServer.Value == true then
				serverStorage:SetAsync(tableToSave, player.UserId)
			end
		end)
		
		if success then
			print("Successfully saved data")
		else
			warn(err)
		end
	else
		print(player.Name.." doesn't own a server!")
	end
end)

Can anybody help? Saving I believe works but I don’t know how to get the data.

1 Like

There are tutorials out there on how to get/save such data.

Can you provide a link? I have been looking for a while and couldn’t seem to find suitable tutorials that fit my situation. It’s more about getting the multiple values of the data rather than saving it.

Save your values in the data in different keys so that you can get each value properly.
You can also watch @Alvin_Blox 's video on Datastores.

1 Like

Hi there,
For my understanding… you want get data right? . All you need to do is to loop all children inside storage(folder) and check if their name is same to the data’s name then change the value to the data’s value. Hope you understand what im trying to say.feel free to ask anything if you dont understand :slight_smile:

Not very sure what you mean by this? I am looping through the folder values and changing the folder values to the data value? How could I do that with the data values?

local success, err = pcall(function()		
		data = serverStorage:GetAsync(player.UserId.."-ServerStorage")
		
		for _, items in pairs(storage:GetChildren()) do
			if items.Name == data.Name then -- i dont know what im doing lol
				print("test")
			end
		end
	end)
local success, err = pcall(function()		
		data = serverStorage:GetAsync(player.UserId.."-ServerStorage")
		
		for i, items in pairs(data) do
			Player.Storage[i] = item-- i dont know what im doing lol
				print("test")
			end
		end
	end)

Try that.

Nothing prints? Not sure what is wrong.

Can i check ur code??

Reeeeeeeeeee

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")

		for i, item in pairs(data) do
			player.Storage[i] = item
			print(i)
		end
	end)
end)

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

			table.insert(tableToSave, player.Storage.OwnsServer.Value)
			table.insert(tableToSave, player.Storage.ServerName.Value)
			table.insert(tableToSave, 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)

Try print i and item .

Reeeeeee

The error is somewhere here (I think):

for i, item in pairs(data) do
	player.Storage[i] = item
	print(i)
end
1 Like

Edit : i think there is problem in ur saving code.u try print the saving value??

The code saves successfully. When I make a server and then leave, there is a success message that my data was saved and no errors occurred.

Ik that . Can you try print the saved data . I want to confirm something

I changed print(i) to print(data) and I got Instance. I think this is because I am saving multiple values and also trying to get multiple files, so it can’t print all of them?

Print(data.Name).try that

Chdjrjfjdj

Try this:

local tableToSave = {
  ["OwnsServer"] = player.Storage.OwnsServer.Value;
  ["ServerName"] = player.Storage.ServerName.Value;
  ["Owner"] = player.Storage.Owner.Value;
}

Nil

Doesn’t seem to be saving. Oddly, it only saves sometimes.