I have an inventory gui where the items in the inventory are added as you buy them. This is the script where they get added into the list:
function FillInventoryList() wait() InventoryList:ClearAllChildren() for i,v in pairs(Player.Hats:GetChildren()) do local List = script.ListItem:clone() List.Parent = InventoryList List.Position = UDim2.new(0,0,0,(i-1)*30) List.Text = v.Name List.Name = v.Name end for i,v in pairs(Player.Faces:GetChildren()) do local List = script.ListItem:clone() List.Parent = InventoryList List.Position = UDim2.new(0,0,0,(i-1+#Player.Hats:GetChildren())*30) List.Text = v.Name List.Name = v.Name end end
I want to connect something to the list item that I have just added, and I want to do it in the same Local Script. How can I access the item in order to write something like this:
Item.MouseButton1Down:connect(function() Item.MouseButton1Down:connect(function() if Player.Hats:WaitForChild("Mesh") and Player.Hats:WaitForChild("Texture") then game.ReplicatedStorage.Requests.WearItem:FireServer(Player.Hats:WaitForChild("Mesh"), Player.Hats:WaitForChild("Texture")) end end) end)
You already have a reference to the created Item: List!
function FillInventoryList() wait() InventoryList:ClearAllChildren() for i,v in pairs(Player.Hats:GetChildren()) do local List = script.ListItem:clone() List.Parent = InventoryList List.Position = UDim2.new(0,0,0,(i-1)*30) List.Text = v.Name List.Name = v.Name List.MouseButton1Down:connect(function() --Assuming 'List' is the button itself. v:WaitForChild("Mesh") --This will yield until it exists, so there's no reason to check that it exists after it does. v:WaitForChild("Texture") game.ReplicatedStorage.Requests.WearItem:FireServer(v.Mesh, v.Texture) --We already know Texture and Mesh exist, no reason to wait for them again. end end) end for i,v in pairs(Player.Faces:GetChildren()) do local List = script.ListItem:clone() List.Parent = InventoryList List.Position = UDim2.new(0,0,0,(i-1+#Player.Hats:GetChildren())*30) List.Text = v.Name List.Name = v.Name List.MouseButton1Down:connect(function() v:WaitForChild("Texture") game.ReplicatedStorage.Requests.ChangeFace:FireServer(v.Texture) --Assuming this is the syntax. end end) end end