Saving a Players Inventory Through One Datastore

I am developing a game where each player has an inventory. Each item has a separate bool value stored in the player. Once the player gets a specific item it changes its bool value to true

How would I go about storing all of the item’s bool value in one datastore rather than a datastore for every item? How would I also go about retrieving all of the items bool value from one datastore?

I could not think how to achieve this and everything I have tried had not worked

This is what the player looks like in-game if it helps:
image

The script that sets up all of the items:

local Players = game:GetService("Players")

local Arrows = {"Default", "Blue", "Red", "Yellow", "Rainbow", "Developer"}

Players.PlayerAdded:Connect(function(Player)
	local Inventory = Instance.new("Folder")
	Inventory.Parent = Player
	Inventory.Name = "Inventory"
	
	local ArrowsFolder = Instance.new("Folder")
	ArrowsFolder.Parent = Inventory
	ArrowsFolder.Name = "Arrows"
	
	for _, a in pairs(Arrows) do
		local newArrow = Instance.new("BoolValue")
		newArrow.Name = a
		newArrow.Parent = ArrowsFolder
		if a == "Default" then
			newArrow.Value = true
		else
			newArrow.Value = false
		end
	end
	
end)

Thanks if you can help <3

The easiest way to accomplish this would be to create a dictionary table where each key is the arrows name and the corresponding value is the bool you have set for the arrow.

local datastore = game:GetService('DataStoreService'):GetDataStore('Arrows', '771417')

local myArrows = { ArrowA = true, ArrowB = true, ArrowC = false }

datastore:SetAsync('test', myArrows)

wait(7)

local test = datastore:GetAsync('test')

print(typeof(test))
print(test)

image

A lot of people don’t realize this because it’s not documented in the devhub wiki, but you can store tables and dictionaries directly using datastores. All data you input into datastore setter methods are converted into JSON before being stored away

Make sure you are being mindful of datastore request limitations and errors as well. More information on this can be found here: Data Store Errors and Limits (developer.roblox.com)

1 Like