Methods don't transfer across RemoteFunctions

I’m using OOP to create an object.

The object is being instantiated well enough (proven by a server-side print) however when I pass the object through a RemoteEvent, all of the attributes to the object are received but the methods appear to be dropped.

Any idea why this happens / is this just a Roblox querk? If so, is there any way to expose server instantiated methods on the client?
Thanks :smiley:

-- Class file (ModuleScript)
local object = {}
object.__index = object

function object:new(name, classes)
    object.Name = name
    object.Classes = classes
end

function object:GetClasses()
    return self.Classes
end

return object
-- Main file (Script)
local objectClass = require(script.Object)
local object = objectClass:new("Object", {"PowerUps", "Shirts"}

print(object.Name)
-- Prints string as expected.
print(object:GetClasses())
-- Prints table as expected.

ReplicatedStorage.GetObject.OnServerInvoke = function()
    return object
end
-- Client file (LocalScript)
local object = ReplicatedStorage.GetObject:InvokeServer()

print(object.Name)
-- Prints string as expected.
print(object:GetClasses())
-- Error: Tried to call a nil value.
1 Like

Unfortunately, as it seems, it is impossible to pass functions through bindable/remotefunctions

Functions

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

It also seems like tables with mixed types (arrays and keys etc.) also don’t get replicated properly.

I suggest you only expose functions individually.

1 Like

So how would I go about looking at the same object in more than 1 script.

For example, say I have a PlayerData object which contains PlayerData.Inventory to be an Inventory object. This inventory object has methods like :AddItem() and :RemoveItem().

I also have a shop script which gets the PlayerData of a player, and wants to run :AddItem() when they purchase something? Do I have to use individual BindableEvents and BindableFunctions? Then what’s the point in doing OOP if I can’t expose any functions anywhere apart from where the object is instantiated?

It’s a temporary solution, but you can set it in _G, and access it elsewhere.

I think the point of OOP is that you can define a class in a moduleScript, and create objects in other scripts

But you can’t access objects themselves between scripts unless you only want attributes and no methods?

with _G everything is the same, at least within the same side.

Anyways, you can pass functions as arguments into an event, but it says that.

Parameters

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

Also, using _G is bad practice, I only showed it as an example

Seems a bit stupid that you can’t share objects between scripts. So what you suggest I do for something like:

PlayerData
:: Inventory
:::: AddItem

for a shop where I get the PlayerData object. Should I just not use OOP and stick to a large number of Bindables for Getting, Setting and Removing data from my Inventory.

I think instead of using runtime-objects, you should serialize your data into roblox Instances, for example, having the inventory as a folder, containing each item as a tool.

Like the backpack for instance.

But I’m not saying to ditch OOP entirely, there are some cases where, within each script, you want to have the same datastructure, but the objects are unique to each script.

@therikald
I know it might be too late now, but you can try using lua serialization libraries, such as lua-users wiki: Pickle Table
Note: Make sure to recreate the metatable __index, as PickleTable does not serialize metatables (I think)

If that doesn’t suit your needs, here is a list of serializers: lua-users wiki: Table Serialization

1 Like