How do I save a players backpack?

Hello! my name is Light (or carlover38)! I am a new developer and am trying to get started on learning the scripting side of developement, I came across something that no tutorials really went into depth about…

My goal is to create a datastore that saves the players entire backpack. My problem is that all the tutorials I find save tools individually and I want the entire backpack to save instead so that I can save other things later on that might not be tools.

Below is the basic script that I have so far:

local DataStoreService = game:GetService("DataStoreService")
local invDataStore = DataStoreService:GetDataStore("invDataStore")

--Load Data
game.Players.PlayerAdded:Connect(function(player)

	local playerUserId = "Player"..player.UserId

	local data
	local success, errormessage = pcall(function()
		data = invDataStore:GetAsync(playerUserId)
	end)

	if success then
		print("Player data loaded successfully")
	else
		print("Player data failed to load")
		warn(errormessage)
	end

end)

--Save Data
game.Players.PlayerRemoving:Connect(function(player)
	local playerUserID = "Player"..player.UserId

	local success, errormessage = pcall(function()
		invDataStore:SetAsync(playerUserId, data)
	end)

	if success then
		print("Player data saved successfully")
	else
		print("Player data failed to save")
		warn(errormessage)
	end
end)

Any help is appreciated, thank you!

1 Like

Using serialization you can encode the items into a string, than you store each item into a table. Than once they join you deserialize the strings representing eaxh item and put them in the backpack.

2 Likes

Thank you, I will look up some tutorials on that specifically and see if I can make it.

Great. I thought i would provide you with some resources to aid your understanding one good article made by @EgoMoose which shows how serialization helps in datastoring things. Its not particularly for your use case however it does cover serialization for storing datatypes here Creating A Furniture Placement System

@starmaq has a article on the use of serialization here.

I really hope that helps. Best wishes, Boston.

2 Likes