Saving player's inventory via DataStores

Hello,
I am making a custom shop GUI for my game where the player can purchase various accessories for their character with a leaderstats value as the currency, and equip any accessory they successfully buy. However, I am very new to making things such as these, and don’t know the best way to save what the player has bought when they leave the game and then join again.

I was thinking about having a folder of bool values correlating to each hat that are toggled according to whether the hat has been purchased or not–and then saving that folder in the datastore when the player left, and going off of that. But that seems much too hacky and inefficient, and is the only idea I could come up with due to my inexperience.

Any suggestion is appreciated! :wink:

Save all the value purchased by the player in a table. How To Make A Roblox Game - Saving Data (4) - YouTube

I already have all of this completed for my leaderstats value so the it saves, but I don’t know a good way to save and load a slew of different kinds of values (stringvalues, boolvalues) in a neat and concise way in the same function as saving the leaderstats. Guess I should have made that a little more clear.

A:

This sounds like a better fit for a PersistenceService. You can learn all about how to use one from this video.
Essentially, you’ll use it to save a data model whenever it changes. Your data model will have a Boolean flag (based on the player’s hat choice) to indicate whether the hat has been purchased or not.
BTW, PersistenceService has been around for ages, so you don’t need to worry about it violating the Rules of Engagement or anything.

I’m not quite sure what you mean, can you elaborate? No clue what PersistenceService even is.

persistenceservice is deprecated

Figured as much as I’ve never heard it been talked about

I already did this.

  1. Dupe items in a folder called “Inventory” inside the Player before they leave.
  2. Save items names in a table.
  3. Dupe the found items in a folder called “Items” inside ServerStorage or smth else when they join.

My game’s Inventory System.

local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("NotPermanentItems")
local DataStoreService = game:GetService("DataStoreService")

local SaveInventoryData = DataStoreService:GetDataStore("InventoryData")

game.Players.PlayerAdded:Connect(function(Player)
	local Inventory = Instance.new("Folder", Player)
	Inventory.Name = "Inventory"
	local InventoryData = SaveInventoryData:GetAsync(Player.UserId)

	
	local Backpack = Player:WaitForChild("Backpack")
	local StarterGear =  Player:WaitForChild("StarterGear")
	

	
	if InventoryData ~= nil then
		for i, v in pairs(InventoryData) do
			if ToolFolder:FindFirstChild(v) then
				ToolFolder[v]:Clone().Parent = Inventory
			end
		end
	end
	
	Player.CharacterRemoving:Connect(function(Character)
		Character:WaitForChild("Humanoid"):UnequipTools()
	end)
	Player.CharacterAdded:Connect(function(Character)
		Player.Character:FindFirstChildWhichIsA("Humanoid").Died:Connect(function(Character)
			Player.Character:FindFirstChildWhichIsA("Humanoid"):UnequipTools()
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local InventoryTable = {}

	for i, v in pairs(Player.Inventory:GetChildren()) do
		table.insert(InventoryTable, v.Name)
	end
	if InventoryTable ~= nil then
		SaveInventoryData:SetAsync(Player.UserId, InventoryTable)
	end
end)
2 Likes

Found something that pretty much answers my question. Saving Player Folder to DataStorage

Here is a video tutorial on how to create a persistent inventory system from scratch: PERSISTENT INVENTORY SYSTEM in Roblox Studio - Lua - YouTube

1 Like