Best way to loop through players asset table

This is what I have

for _, assets in pairs(PlayersHumanoid.assets) do
	for i, asset in pairs(assets) do
		if type(asset) == 'table' then 
			for _, assetType in pairs(asset) do
				if assetType == 'Face' then
					print(asset.name)
				end
			end
		end
	end
end

If the asset table was like

assets = {
	{
		id = 28999228,
		assetType = {
			name = "Face",
			id = 18
		},
		name = "Joyous Surprise"
	},
)

And I want the print() to print ‘Joyous Surprise’, but it’s printing ‘Face’ instead. Any ideas to clean this up and get it working properly??

You are also looping through the tables inside { }. Remove:

for _, assetType in pairs(asset) do

end

And it should print Joyous Surprise.

That wouldn’t work cause then I’d get this

for _, assets in pairs(PlayersHumanoid.assets) do
	for i, asset in pairs(assets) do
		if type(asset) == 'table' then 
			if assetType == 'Face' then
				print(asset.name)
			end
		end
	end
end

And I’d have no way of knowing what ‘assetType’ actually is

Rezault is on the right lines, but a further change has to be made in order for it to work. In your initial code, the line for _, assetType in pairs(asset) do is actually iterating through the keys and values of an asset. So that for loop can be removed.

When it’s in a dictionary structure, you can index it by the keys of the assets. This means to get the assetType of an asset you can simply use asset.assetType.name. Currently, in your revised code with the change implemented you’re comparing an undefined variable with ‘Face’.

With this change reflected in your code you get:

for _, assets in pairs(PlayersHumanoid.assets) do
	for i, asset in pairs(assets) do
		if type(asset) == 'table' then 
			if asset.assetType.name == 'Face' then
				print(asset.name)
			end
		end
	end
end

And this will now print your desired results.

[attempt to index field ‘assetType’ (a nil value)]

for _, assets in pairs(PlayersAppearance.assets) do
	for i, asset in pairs(assets) do
		if type(asset) == 'table' then 
			if asset.assetType.name == 'Face' then --Error here
				print(asset.name)
			end
		end
	end
end

Are you sure the structure of your assets dictionary in PlayerAppearance are actually in the structure you described?

local PlayersAppearance = Players:GetCharacterAppearanceInfoAsync(Player.UserId)

for _, assets in pairs(PlayersAppearance.assets) do
	for i, asset in pairs(assets) do
		if type(asset) == 'table' then 
			if asset.assetType.name == 'Face' then
				print(asset.name)
			end
		end
	end
end

Doesn’t really matter cause just found out freaking InsertService doesn’t work from the client anyway :roll_eyes:

My bad, I just realised you’re iterating through PlayersAppearance.assets. The following iterating is unnecessary as there’s no need to iterate through each item in the assets dictionary.

for _, asset in pairs(PlayersAppearance.assets) do
	if type(asset) == 'table' then
		if asset.assetType.name == 'Face' then
			print(asset.name)
		end
	end
end

As for your problem requiring InsertService not working from the client, would you not be able to just do all this work on the server and then pass the assets to the client after being inserted?

1 Like

That’s now working :smiley:

And ye that’s probably what I will do. Just frustrating, as it will require remoteevents and stuff to be called and items to be stored somewhere temporarily, etc. was hoping for just a simple insert onto the model bam and be done

1 Like