What I want it to do:
I’m trying to print the child name when an ImageButton in Upgrades is clicked.
Why do I want to do this?
I am trying to do this so I can create a Selecting system for all the ImageButton’s (slot selection).
function GuiService:SelectUpgrade(obj)
print(obj)
end
Client.UpgradeFrame.Shop.Container.Upgrades.ChildAdded:Connect(function(child)
if child:IsA("ImageButton") then
child.MouseButton1Click:Connect(GuiService:SelectUpgrade(child))
end
end)
It gives me this, Error:
01:32:09.820 - Attempt to connect failed: Passed value is not a function
That does not solve what I want to achieve though, how is it supposed to define/index “child”? I probably need to connect the ChildAdded & MouseClick1 together somehow.
If I do the below, how could I pass the child name/value from :Connect?
local function exampleFunction(parameters)
print(parameters) -- I want it to print the clicked "child's name"
end
Client.UpgradeFrame.Shop.Container.Upgrades.ChildAdded:Connect(function(child)
if child:IsA("ImageButton") then
child.MouseButton1Down:Connect(exampleFunction) -- need it to pass the child's name/value.
end
end)
for _, button in pairs(Client.UpgradeFrame.Shop.Container.Upgrades:GetChildren()) do
if button:IsA("ImageButton") then
button.MouseButton1Down:Connect(exampleFunction)
end
end
That is interesting, I’ll take a look into closures right now.
That was the solution. Here is it if anyone is interested:
function GuiService:SelectUpgrade(obj)
return function()
print(obj)
end
end
Client.UpgradeFrame.Shop.Container.Upgrades.ChildAdded:Connect(function(child)
if child:IsA("ImageButton") then
child.MouseButton1Down:Connect(GuiService:SelectUpgrade(child))
end
end)