Hello everyone,
I’m working with an inventory gui and I’m trying to get the buttons to work. Currently I loop through all the buttons in the inventory frame and make a .MouseButton1Click connection to them, but for some reason regardless of the button, it passes for a different button. Let me explain.
ui.CharacterRoomUI.BottomButtonsFrame.GiftButton.MouseButton1Click:Connect(function()
inventoryFrame.Visible = true
local f = inventoryFrame.Frames.FoodFrame.ItemsFrame:GetChildren()
for i = 1, #f do v = f[i]
if(v:IsA("TextButton"))then
warn("APPLE OR BANANA?",v)
insert(connections,v.MouseButton1Click:Connect(function()
warn(v)
inventoryFrame.Visible = false
end))
end
end
end)
The output is as follows:
“APPLE OR BANANA?” Apple - Client - Inventory UI:30
21:16:12.675 “APPLE OR BANANA?” Banana - Client - Inventory UI:30
21:16:13.341 Banana - Client - Inventory UI:32
Regardless whether or not I click on the apple button, the v.MouseButton1Click connection outputs Banana.
I need assistance on to why this happens and how could I possibly fix this,
insert is just a local table.insert, connections is a table of connections for the inventory buttons → these are disconnected when the inventory is closed.
Thanks for the feedback
Another note is that when I manually delete the banana button, it properly outputs apple.
i do use a ui list layout but I attempted without it and it still didn’t work so I really don’t know what I’m doing wrong
I would recommend using .Activated in place of MouseButton1Click to cater for users on mobile devices.
I don’t see any immediate reason why it should give the same output. I use a variation of yours to achieve the same result with no issues:
for i, v in pairs(<insert_form_Name>:GetDescendants()) do
if (v:IsA("TextButton")) then
v.Activated:Connect(function()
print("Button clicked", v.Text,)
end)
end
end
inventoryFrame:GetPropertyChangedSignal("Visible"):Connect(function()
if(inventoryFrame.Visible)then
else
for i = 1,#connections do
connections[i]:Disconnect()
end
end
end)
Thanks for the tip, and same I have no clue why exactly it’s not working properly functioning
Makin me tear my hair out
When I use three buttons, apples, bananas, and pancakes, no matter which button I press it only outputs pancakes, which is the last button to be created so it’s always going to be the last button that’s outputted
Edit:
I fixed it by using a for, next loop instead
Im still confused as to why the original for i = 1,#f do doesn’t work but it’s whatever since I found the solution
Thanks for the help though
I wanted to use that type of loop since it’s supposedly faster but since there isin’t much to loop through atm it shouldn’t be too much of a problem
I do it since I have a lot of gui elements that can have connections on them and I never really learned whether or not its ok to have a lot of :Connect() stuff or if it will eventually start to lag
If you do know could you educate me on it?
I’ve always kept performance in mind to cater to lower end devices
We could get into more detail with this in private messages or something similar, perhaps through discord?
I meant that I had never done Disconnects before, but I can see the reason to do it and it looks like a good idea to do that when you close any GUI. That’s why I like the forum so much, cos you learn from others all the time with simple little code pieces like that.