Ambassador is an open source library that lets you send arbitrary data over from the server to the client, and vice versa.
Currently, you can send strings, numbers, booleans, tables (no metatables), functions, enums, and instances. Always add sanity checks, even when using Ambassador.
NOTE: A wrapper is generated when a function is sent. As such, the received function will yield, since it makes a call to InvokeServer or InvokeClient.
Why not just use RemoteFunctions and RemoteEvents?
Ambassador uses RemoteFunctions under the hood and builds on them, by encoding data that cannot be sent through regular RemoteFunctions. Instead of setting up remotes, keeping track of them, invoking them, and encoding data manually, Ambassador does all of that for you. All you need to do is invoke it with a name, and a piece of data.
Example
We can send a function from the client to the server which, when called, will run locally and set the time to whatever the server requests:
Ambassador:Send("setLocalTime", function(time)
game.Lighting.TimeOfDay = time
end)
On the server, we want to receive this setLocalTime
function and invoke it. In case of an error, we will kick the player. When the player leaves, we also have to invoke Ambassador:Cleanup
to clean up any player-specific remotes that the library created.
game.Players.PlayerAdded:Connect(function(player)
local success, result = Ambassador:Await("setLocalTime", player)
if success then
-- if the call succeeds, result stores the setLocalTime function
result(math.random(0, 24))
else
player:Kick(result) -- result stores the error in case of a failure
end
end)
game.Players.PlayerRemoving:Connect(function(player)
Ambassador:Cleanup(player) -- Clean up any left-over remotes
end)
Using Ambassador
Rojo
Clone the git repository into a folder, e.g. src/lib
. Then, require it as you would any Roblox module.
Studio
Copy the contents of init.lua
and paste them into a ModuleScript in ReplicatedStorage. Then, require the ModuleScript for use in your scripts.
Sanity checks are still required just as they always were. Ambassador is a quality of life improvement, not a security improvement.
Documentation can be found here.
Please let me know if you encounter any bugs or have any further questions.