PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Invoke

Invoke will call the OnInvoke callback and return any values that were returned by the callback (if any). If the OnInvoke callback is not set, this method will yield until one is set. If OnInvoke yields, this method will also yield. There are limitations on the values that can be sent as arguments; see the code samples.

Parameters

Name Type Default Description

Returns

Return Type Summary

Code Samples


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";
}

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))