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

How does one pass a connect line optional parameters with a named function?

I'm trying to call a custom function through a button click connection and I want to pass an argument to the function but I'm getting an error that says the passed value (AddQuant('Food')) is not a function. What do I need to do to pass that argument? I don't want to use an anonymous function, and I've already checked spelling and capitalization. Anyways, here's the connection line and function.

--The function
function AddQuant(type)
	amount[type] = amount[type] + incriment[type]
	Labels[type].Text = "Food: " .. amount[type]	
end
]
--The connection line
Buttons['Food'].MouseButton1Click:connect(AddQuant('Food'))

Thanks for any help.

-GoldenPhysics

Edit: added function being called.

1 answer

✓ Accepted answer

When you connect an event, it adds its own parameters based on the event. Attempting to add your own via the connection line will produce an error. A solution is to call your named function with an anonymous function, like this:

--The function
function AddQuant(type)
    amount[type] = amount[type] + incriment[type]
    Labels[type].Text = "Food: " .. amount[type]    
end
]
--The connection line
Buttons['Food'].MouseButton1Click:connect(function()
    AddQuant('Food')
end)

1 comment

0

I was hoping for a shorter connection line, but It works well. Thanks!

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