Trying to create a loop with values from a table

Trying to create a loop which goes through values of a table then using those values finds part of a GUI with the same name to alter it.

Currently I have a manual script which works but has to be done per value:
image
Once I add more weapons it would be long having to go through every item to find it

So I thought I would create a loop to go through every part of the table and if it finds that name in the gui then it can alter it, currently it looks like this:
image

Which returns this error:

The print which can be seen at the top of the first image of script looks like this:
image

This table is sent to the client from a server script which is made like this:


(Copies owned items into a new folder inside replicated storage so the client can read it, before deleting the folder)

Im assuming this error is down to how the table is printed out ([“M4A1”] = M4A1 rather than just M4A1) but ive tried messing around with this part of the script and changing the last line (Inventory1Owned[Item.Name] = clientItem) but have only got errors when doing so

So for the table being sent to the client you are setting the Index to the item name and also the value. To fix this you should do

table.insert(Inventory1Owned, Item.Name)

as for the error make sure to use :WaitForChild() or check if :FindFirstChild() exists

for Index, Value in pairs(Inventory1Owned) do
   local WeaponExist = mainframe.Weapons:FindFirstChild(Value)
   if not WeaponExist then continue end -- Doesn't exist
   WeaponExist.owned.Visible = true
end
1 Like

Edit: Now that I look at it more, I don’t think I saw the problem correctly. I’m going to leave this up anyways just in case.

If inventory1Owned is a table, remove :GetChildren() call within the for loop. pairs() takes the table value. You only use :GetChildren() if you are working with a roblox object, and needing to get the list of its children from the explorer hierarchy.

so like this:

for i, v in pairs(Inventory1Owned) do
-- code
end

Hope this helps!

1 Like