How would I go about passing a custom argument when calling a function that returns no default argument

I’m currently writing a fairly simple script which controls a vote system.

I’m not actually having a problem with the script itself, but I want to try and make this script smaller by
passing my own argument from an event that has no default argument

Example of what I mean:

function starClicked(star)

stars.Star1.MouseButton1Click:Connect(starClicked(stars.Star1.Name) --[[I want this to pass to the above function, but I can't seem to get that to happen.]])

Any help on getting something like that to work would be fantastic as it would help me further optimize any script I make as I’ve had this issue before but I countered it with longer coding. Thanks :smiley:

What you’re looking for is an anonymous function.


That’s exactly what I’m trying to avoid so I can shorten scripts for optimization reasons.
I made optimizing my code a goal some time back as some of my previous work isn’t very optimized, and I ran into this issue.

If there’s any workaround, I would appreciate it!

Basically I’m trying to tie a few buttons to one function that also knows which button was clicked via an argument.

There is no alternative that would be more optimal than an anonymous function. If there was, it’d be a very very minute difference that wouldn’t be noticable.


Alright, thanks for clarifying this to me.

I feel like this feature should be added, but I don’t know how this could be added by ROBLOX.

Again, thank you for clarifying this.

Here is an alternative suggestion that you may find makes your code a little cleaner if you are repeating this kind of thing for various objects:

local function hookButton(button)
    return button.MouseButton1Click:Connect(function()
        -- event handler body

-- use return value if you may want to disconnect

Lovely bit of code that you sent.

Aye this is a useful bit of code and I’ll base stuff I write around that in scenarios like mine.
Thanks for the help!

I’m not sure how less code == better optimisation. Seems more like a useless micro-optimisation which has a negligible difference. I wouldn’t worry too much about shortening your code over being productive.


I understand what you’re saying, and yes I agree it is very minor. However, this will also save time with my programming as it requires less repetitive code which seems to give me a bit of brain fog sometimes when I code, which in turn reduces my productivity drastically. Besides, every little helps, can’t hurt to micro-optimise things here and there. Each line of code takes about 1ms for Lua to read doesn’t it?