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