Do retriever functions in moduleScripts only work on one side?

Okay, the title is a bit lackluster, so let me elaborate.

I have tried making a sample moduleScript for fun to practice OOP (Object Oriented Programming), and I wanted to create a Retriever function. Basically all objects on creation are put into a table, and that table is being searched through with that function, taking a unique ID as a parameter.

function sample.GetObjectById(id:number)
	local found = nil
	
	print(STORAGE) -- The storage table (prints out nil)
	
	for i, object in pairs(STORAGE) do
		if (object.Identifier == id) then
			found = object
			break
		end
	end
	
-- In this case, the object.Identifier is a property of the object

	return found
end

Basically finding the same value of a property that was inputted as a parameter.

However, I found that the client isn’t aware of objects created by the server. Therefore, it sees the storage table as nil (see the print above). Am I doing something wrong or is this intended behavior? I would also appreciate possible workarounds for this, for potential future modules.

TL:DR - The function in an OOP module script, when called from the client, isn’t aware of the things happening on the server.

Any help appreciated!

That is intended behavior; when the client and server require a ModuleScript, they receive two different versions of the ModuleScript


Yea, but be mindful that metatables don’t transfer over the client/server boundary

2 Likes

Thank you for your answer!

Does this mean that I have to send the data via RemoteEvents in these types of situations?

Sorry to bother you again, but what does that exactly mean? Do values become nonexistent when reaching the client?

Let’s say you have a table set up like this:

local t = setmetatable({k=1}, {__call=print})

If you tried to pass it from one boundary to the next like so:

FireServer(t)
-- or
FireAllClients(t)

The metatable won’t exist on the other end:

OnServerEvent:Connect(function(_, t)
    print(getmetatable(t)) --> nil
end)

OnClientEvent:Connect(function(t)
    print(getmetatable(t)) --> nil
end)

This roblox article has more info

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.