Passed value is not a function/

hello all, I am making this script to test out some stuff with attributes and try to make a helper for ui in the future, butI’m struggling because when I call

uiObject.MouseButton1Click:Connect(directButton(direction))

I get an error, Which I know has something to do with the function being called because of the parantheses but I don’t quite know how else to pass the value that I need from one function to the other

local Players = game:GetService("Players")

local player = Players.LocalPlayer

local playerUI = script.Parent

local menuUI = playerUI:WaitForChild("menuTest")
local menus = playerUI:WaitForChild("menus")

local function directButton(direction)
	if direction then
		for i,menu in pairs(menus:GetChildren()) do
			if menu.Name == direction then
				menu.Visible = not menu.Visible
			end
		end
	end
end

local function checkForButtons(checkUI)
	for i,uiObject in pairs(checkUI:GetChildren()) do
		if uiObject:IsA("ImageButton") or uiObject:IsA("TextButton") then
			local direction = uiObject:GetAttribute("menuDirect")
			uiObject.MouseButton1Click:Connect(directButton(direction))
		else
			checkForButtons(uiObject)
			local test
		end
	end
end

checkForButtons(menuUI)

Its difficult to explain but you’ve already ran the function before you connect it.

To visualise

local thing = directButton(direction)

uiObject.MouseButton1Click:Connect(thing)

Instead you need to set direction to a global variable and put .MouseButton1Click(directButton)