Issue with Avatar Inventory system

Hello, I am trying to make an avatar inventory system for my game. Basically, when the person accesses the UI, they would see all assets from their character in the UI, like this.

I have two issues with these, first, whenever I open the inventory, it will only get the assets loaded at the time the UI is loaded ( when the player joins the game ), and whenever I add a new asset it will not update and if I only define it inside the open button function, asset is nil so it shows nothing.

My code for this is

-- inside the button script
button.MouseButton1Click:Connect(function()
     assets = Players:GetCharacterAppearanceInfoAsync(player.UserId)
end)

-- outside the button script
local assets = assets = Players:GetCharacterAppearanceInfoAsync(player.UserId)

pcall(function()
	for _,v in pairs(assets.assets) do
		for a, i in pairs(v) do
			if type(i) == "string" then
				name = i
			end
			if type(i) == "number" then
				imageid = "https://www.roblox.com/asset-thumbnail/image?assetId="..i.."&width=420&height=420&format=png"
			end
		end
		setGrid(imageid, tostring(name))
	end
end)

My setGrid function is

function setGrid(imgid, labeltext)
	local newGrid = template:Clone()
	newGrid.Parent = maincontent
	newGrid.Visible = true
	newGrid:FindFirstChild("ImageLabel").Image = imgid
	newGrid:FindFirstChild("TextLabel").Text = labeltext
end

Now, my second problem is, I am trying to get the button to remove the asset when clicked, since my setGrid is a function which clones a template and such, I don’t know how to get the asset from that previous script, which is an issue since I want to get the asset to then be able to send an event to the server saying remove this please.

I’ve tried looking online for ways to fix it but can’t seem to find any, and I genuinely don’t know how to make it so I could get the asset any other way.

1 Like

First of all with your first problem add a
CharacterAdded function to make sure the character is fully loaded

Then regarding your 2nd problem I usually make a mousebutton1 function in the same function as cloning it as such

function setGrid(imgid, labeltext)
	local newGrid = template:Clone()
	newGrid.Parent = maincontent
	newGrid.Visible = true
	newGrid:FindFirstChild("ImageLabel").Image = imgid
	newGrid:FindFirstChild("TextLabel").Text = labeltext
newGrid.RemoveButton.MouseButton1Click:Connect(function()
--do something
end)
end
1 Like

Your MouseButton1Function inside the function of cloning it was smart and I am definitely going to use it, however, the CharacterAdded function does not fix it. The asset does register, the problem is, it doesn’t update, and whenever I add it back onto the open GUI function, I do asset = bla bla bla to update it, but it still doesn’t.

1 Like

Try adding a child added event detecting if an accessory etc was added?

also sorry for the late response, i only just opened devforum up

I just made it check in a for loop for shirt/pant/tshirt and accessories, then I used a few checks and changed up the function a bit, however, I’m still gonna give you the solution since you did help with the button function.