Help on inventory GUI

I’m trying to achieve a working and optimized inventory.
unfortunately I just recently started using profileservices to store data so I still have a lot to learn.

in the line:
21,22,23>> StarterPlayerScripts.Client.PetInventory

Pets.ChildAdded:Connect(function()
	UpdateInv()
end)

This would work in a normal DataStore system but unfortunately because profile services stores the data elsewhere this line doesn’t work.

I tought of 2 main solutions but i don’t know how to do any of them:
Either update the PetsFolder Function in DataHandler Script to make it so it checks the data every second and updates the folder[UNOPTIMIZED?]:

local function PetsFolder(player)
	local profile = Profiles.Profiles[player]

	if not profile then return end

	local PetsFolder = Instance.new("Folder", player)
	PetsFolder.Name = "Pets"
	
	for i, v in pairs(profile.Data.Pets) do
		local Pet = Instance.new("BoolValue", PetsFolder)
		Pet.Name = v
	end



end

local function PlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
	if profile ~= nil then
		profile:AddUserId(player.UserId) -- GDPR compliance
		profile:Reconcile() -- Fill in missing variables from ProfileTemplate (optional)
		profile:ListenToRelease(function()
			Profiles.Profiles[player] = nil
			-- The profile could've been loaded on another Roblox server:
			player:Kick("Data issue, try again shortly. If issue persists, contact us!")
		end)
		if player:IsDescendantOf(Players) == true then
			Profiles.Profiles[player] = profile
			-- A profile has been successfully loaded:

			
			GiveLeaderstats(player)	
			PetsFolder(player)
		else
			-- Player left before the profile loaded:
			profile:Release()
		end
	else
		-- The profile couldn't be loaded possibly due to other
		--   Roblox servers trying to load this profile at the same time:
		player:Kick("Data issue, try again shortly. If issue persists, contact us!") 
	end
end

Or Change the PlayerInventory script.

local Player = game.Players.LocalPlayer
local Pets = Player:WaitForChild("Pets")
local ReplicatedStorage = game.ReplicatedStorage
local ProfileManager = require(game:GetService("ServerScriptService").PlayerData.ProfileManager)
local UI = Player:WaitForChild("PlayerGui"):WaitForChild("Inventory")

local function UpdateInv()
	for i, v in pairs(Pets:GetChildren()) do
		if  UI.Frame.Main.Pets:FindFirstChild(v.Name) then
		else
			local T = script.Template:Clone()
			T.Parent = UI.Frame.Main.Pets
			T.Name = v.Name
		end
	end
end

UpdateInv()

Pets.ChildAdded:Connect(function()
	UpdateInv()
end)

Thank you for reading.

1 Like

This could be quite heavy for the server because remember, all ValueBase (BoolValue, StringValue) instances you create are replicated to all players.

Meaning that, if you store 36 Instances per player, that would be equivalent to 36 * total_players. An equivalent of 864 Instances for 24 players. Now imagine, if you needed to use 10 .Changed events in the server, that would be 240 .Changed connections. ValueBase objects are still much better than Attributes though.

To minimize the impact, you’d only listen for changes in the client. However, there is also the price of replication for these objects.

36 * total_players is also replication cost. Whenever you change the value in the server, it has to replicate to all players even if the data is only meant for a single player.

Meaning, that ValueBase for data management can already be inefficient by itself.

You should learn how to use dictionaries for data management (consider reading articles about CRUD dbms) + Remote events, because RemoteEvents give you more control on who gets replication and who doesn’t . e.g. FireClient()

You can also just use ReplicaService + ProfileService which already does the CRUD method for you. That would be more efficient yes.

Then it’s just a matter of updating an inventory table in the profile data:

local pets = {}

-- replica service makes this process prettier with Replica:SetValue()
add(player, 'pets', 'jaguar', 1)
increment(player, 'pets', 'jaguar', 10)
delete(player, 'pets', 'jaguar')

Thank you for responding, ill’look into it.:+1:

1 Like