Passed value is not a function?

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

You’re calling a function on an instance. Instead, make your own function.

local function thing()
    --Code
end

child.MouseButton1Click:Connect(thing)

Or do this.

child.MouseButton1Click:Connect(function()
    --Code
end)
1 Like

Make GuiService:SelectUpgrade return a function.

function GuiService:SelectUpgrade(obj)
     return function()
          print(obj)
          --// closure's functionality
     end
end

Here is a lua page on closures.
https://www.lua.org/pil/6.1.html

Closures are pretty useful.

1 Like

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.

:white_check_mark: 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)
1 Like