PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

OnInvoke

BindableFunction

Tuple

This callback can be set multiple times, but cannot be called directly. It is called when the BindableFunction/Invoke method is called, using the same arguments as parameters.

Limitations

### Subscription Only one function can be bound to `BindableFunction/Invoke` at a time. If you assign multiple functions, only the last one assigned will be used.

Parameter and Return Limitations

Any type of Roblox object such as an Enumeration, Instance, or userdata can be passed as a parameter/return. Lua types such as numbers, strings, and booleans can also be passed, although there are some limitations on how data can be passed.

Mixed Tables

If a Table is passed as an argument/return it must be an array without missing entries or have string keys, not a mixture, or else the string keys will be lost.

Avoid passing a mixed table (some values indexed by number and others by key), as only the data indexed by number will be passed. For example, when the server receives the colorData table illustrated below, it will only see indices 1 and 2 containing "Blue" and "Yellow" while the other data will be lost in the transfer. Note, however, that sub-tables do not need to be indexed in the same way as their parent — in other words, as long as each individual sub-table is indexed with the same type, all of the data will be preserved.

Non-String Indices

If any indices of a passed table are non-string type (Instance, userdata, function, another table, etc.), those indices will be converted to a string.

-- Mixed table
local colorData = {}
colorData[1] = "Blue"
colorData[2] = "Yellow"
colorData["Color1"] = "Green"
colorData["Color2"] = "Red"

-- Table with two key-indexed sub-tables
local playerData = {}
playerData["CharData"] = {
	-- All children indexed by key
	CharName = "Diva Dragonslayer",
	CharClass = "Knight"
}
playerData["Inventory"] = {
	-- All children numerically indexed
	"Sword",
	"Bow",
	"Rope"
}

Functions

Functions passed as parameters/returns will not be replicated, therefore making it impossible to use these objects to pass functions between scripts.

Parameters

Name Type Default Description

These are the parameters sent when the BindableFunction is Invoked.

Expected return type:

Return Type Summary

Code Samples


BindableFunction Addition

This code sample sets the OnInvoke callback of the parent BindableFunction to a function that returns the sum of two numbers. Test this code sample by pasting this code inside a Script within a BindableFunction. You’ll then be able to use Invoke on the BindableFunction from other scripts.

local bf = script.Parent

-- Define a function for use with the BindableFunction
local function AddTwoNumbers(a, b)
	return a + b
end

-- Set the OnInvoke callback to be our function
bf.OnInvoke = AddTwoNumbers

-- Invoke the BindableFunction
print(bf:Invoke(25, 44))

BindableFunction Valid Values

This code sample shows the kinds of values that can be sent to and from BindableFunctions via Invoke/OnInvoke. Test this code by placing a Script within a BindableFunction inside the Workspace or ServerScriptService. It will raise errors on the values that cannot be sent.

local bf = script.Parent

-- Dummy function
bf.OnInvoke = function (...) return ... end

-- These values CAN be sent to/from BindableFunctions
bf:Invoke()           -- nil
bf:Invoke(25)         -- numbers
bf:Invoke("hello")    -- strings
bf:Invoke(true)       -- booleans
bf:Invoke("buy", 25)  -- multiple values are OK
bf:Invoke{1, 2, 3}    -- tables as arrays with no gaps
                      -- note the curly braces
bf:Invoke{            -- tables with string keys only
	hello = "world";
	goodbye = "world";
}
bf:Invoke{            -- tables with string keys
	point = {1, 2};   -- whose values are also valid
	point2 = {3, 4};
}
bf:Invoke{            -- tables as arrays
	{1, 2, 3};        -- whose values also are valid
	{hello = "world";};
}

-- These are some values you CANNOT send to/from BindableFunctions
bf:Invoke{1, nil, 3}  -- tables as arrays cannot have nil gaps
bf:Invoke{
	[{}] = "hello";   -- table keys can only be numbers OR strings
}
bf:Invoke{            -- tables keys cannot be BOTH numbers AND strings  
	[1] = "apple";
	hello = "world";
}