Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

GUI button connecting and passing a function?

Hi Guys, I have an issue here. I have a script that uses a for loop to capture my button presses for my gui as I am creating multiple buttons via a script. the thing is, I need to know how to pass a value through as well, but i keep on getting an error "Attempt to connect failed: Passed value is not a function" when i first load the game, when the script first runs.

for i,v in pairs(list:GetChildren()) do
		v.MouseButton1Click:Connect(ButtonPress(v.Selection.Value))
	end

when i press the button, any button i am getting a "attempt to call a nil value" error. any help guys?

Inside all of these buttons is a string value called "selection" - and when i press these buttons, i want it to return the value of selection that is in that specific button.

1 answer

✓ Accepted answer

This is because :Connect() is expecting a function to connect to, and to call when it's listener fires. You're trying to call a function in an argument essentially. The function is expecting this format:

MouseButton1Click:Connect(ButtonPress)

It is frustrating at times, since this would be very efficient if we could specify arguments when connecting functions, but this is unfortunately not the case. MouseButton1Click will call the function on it's own and pass the arguments it provides. If your function rely's on personal-set arguments, you'll have run it accordingly:

MouseButton1Click:Connect(function()
	ButtonPress(v.Selection.Value)
end)

1 comment

0

Wonderful, thank you! it just goes to show that coding at 4am isnt my smartest of ideas :D

This discussion is preserved as it was. New answers, comments, and votes are closed.