How to pass a callback as an argument?

Hello everyone,

I’m working on a generic dialog system, so basically I have a ModuleScript that takes:

function NPCDialogue.openDialog (player, npcImage, npcName, npcSpeak, options)

Now, the last argument (options) was designed to be a table with an structure like this:

local options = { { 'Hello', HelloFn }, { 'Close', CloseFn }}

In short it’s an array of buttons, each element in this array would also be an array containing the button label and a callback function.

To define my callbacks, I’m doing something like this:

local HelloFn = Instance.new('BindableFunction')
HelloFn.OnInvoke = function () 
  print('Hello'
end

If I try to print options[1][2] on the script where I’m defining options I get a function as result, but inside my Module Script method options[1][2] is equal to nil.

First thing, I’m not sure if it’s the right way to do, but if it is, then what am I doing wrong?
If not, how am I supposed to do it?

Thanks in advance.

EDIT - May be relevant:
I’m firing an event that is captured by a local script

evtOpenNPCDialogue:FireClient(player, npcImage, 'npcName', 'npcText', options)

When I try to print options[1][2] on my local script the response is also nil.

You can’t pass over functions through remotes as they don’t serialize. Instead have all the functions accessible in a shared container such as ReplicatedStorage, if both server and client need it.

2 Likes

And then OP passes them through a remote

There is surely a better way of doing it, like exposing the functions through accessible bindables/modules

1 Like

I missed the edit, my bad. In my defense, I’m still a bit confused on how the structure of the module, bindable, and remote work. OP, more code might help, as well as explaining how everything works together.

3 Likes