How can I make a button run a function with parameters?

I’m trying to make a GUI for my game but ran into an issue where the script does not run the code but broadcast an error instead.

local parent = script.Parent

local function onButtonActivated(pressed)
	-- script goes here
end

parent.Build.MouseButton1Click:Connect(onButtonActivated(1))

I know that the error occurs at the final line because it has a parameter, but do not know how to deal with this issue. Is there any way around this?

3 Likes

You can’t pass parameters to a function inside of Connect
change

parent.Build.MouseButton1Click:Connect(onButtonActivated(1))

to

parent.Build.MouseButton1Click:Connect(onButtonActivated)
1 Like

The issue with that is the fact that I plan to add more buttons that will lead to that same function, and I need a way to detect which button was pressed in the function

1 Like

Nvm I found a solution. Instead of running the function itself in the connect change I can run a seperate function that runs onButtonActivated

parent.Build.MouseButton1Click:Connect(function()onButtonActivated(1)end)
1 Like