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