How would clients access objects created by the server? [OOP]

Hello!

Goal:
I am new to object-orientated programming and currently trying to find a way for the client to get data from objects created by the server.

Strategy:
I made a table that adds all the created objects when a character spawns and allows the player to get the data with a remote function with the object’s name and function name to call as the parameters.

Issue:
The problem is every time I invoke the server it says the global variable “self” returns nil. I have search through multiple forum topics but still no fix.

Scripts:

Server:

local ps = game.Players
local carClass = require(game.ReplicatedStorage.CarClass)
local func = game.ReplicatedStorage.RemoteFunction

ps.PlayerAdded:Connect(function(p)
	local objs = {}
	p.CharacterAdded:Connect(function(c)
		-- Variables
		local humanoid = c.Humanoid
		
		-- Objects
		local newCar = carClass.new()
		objs.Car = newCar
		
		-- Events
		humanoid.Died:Connect(function()
			for i,v in pairs(objs) do
				v:Remove()
			end
			objs = {}
		end)
		func.OnServerInvoke = function(player, params)
			-- Player is trying to get data.. 
			local name = params.Name -- Object name
			local funcName = params.Function -- Function name
			
			local object = objs[name] -- Finding the object
			if object and object[funcName] then -- If both the object and function exist.. 
				return object[funcName]()
			else
				if object and not object[funcName] then
					warn('THERE IS NOT FUNCTION D;')
				else
					warn('THIS OBJECT DOESNT EXIST D;')
				end
			end
			return nil
		end
	end)	
	
end)

Local Script:


local func = game:GetService("ReplicatedStorage"):WaitForChild("RemoteFunction")

local makeYear = func:InvokeServer({
	Name = "Car";
	Function = "GetYear"
})

print(makeYear)

Module:

local CarClass = {}
CarClass.__index = CarClass

function CarClass.new() 
	return setmetatable({
		-- Memebers:
		Model = "Toyota";
		Made = 2013
	},CarClass)
end

function CarClass:GetYear()
	return self.Made
end

function CarClass:Remove()
	self.Model = nil
	self.Made = nil	
end

return CarClass

Error:

1 Like

Have you tried setting the metatable to self and then returning self in the constructor function?

Yes, and I got the same error.

print object before line 29 for me. It should come up with it’s contents/functions in a dictionary format.

Yea I see what you mean. I’ve also tried directly calling the objects :GetYear() function with the given object’s name e.g( object:GetYear() “object” would be objs[name] ) But when I try calling the function with the given function name it returns the error. e.g ( object(funcName) (args) )

Change your return statement from

return object[funcName]()

to

return object[funcName](object)

Because you are not calling the function with the colon operator, the function will not pass in itself as a parameter, so you must manually add itself.

1 Like

Make sense. Thank you so much!

1 Like

This isn’t an easy thing to do because remotes strip the metatables and all functions.

You’d have to maintain two separate copies of the object on both the server and client, then use some sort of state replicator to keep the two’s props in sync.

1 Like

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