How can I transfer an object's methods?

Hey, dev forum I have a script here that will create a player object server-side. I then have a client script use an endpoint to grab the player’s object. This works but it won’t transfer methods only the variables that I defined such as ‘_health’ etc. Heres a sample of my code;

server:

function PlayerService.Client:getPlayerObject(player)
    return players[player]
end
local player_added = game.Players.PlayerAdded:Connect(function(player)
        players[player] = self.Modules.PlayerClass.new(player)
end)

client

self.player_object = self.Services.PlayerService:getPlayerObject(self.Player)

whats erroring

self.player_object:staminaSubtract(50)

err message;
Players.ZombieKicker7.PlayerScripts.Aero.Controllers.PlayerController:129: attempt to call a nil value

so the problem is that the methods or functions defined inside of my player_object won’t transfer from server to client. Any ideas on how I could fix this?

Modifications to ModuleScripts will only persist for the contexts in which they are required. In other words you’ll need to explicitly handle both server & client replication. Here’s an example where a change is performed exclusively on the client.

--MODULE

local module = {}
module.constant = math.pi
return module
--LOCAL

local replicated = game:GetService("ReplicatedStorage")
local module = replicated:WaitForChild("ModuleScript")
module = require(module)
module.constant = "Hello world!"
print(module.constant) --Hello world!, (explicit client change replicated to the client).
--SERVER

local replicated = game:GetService("ReplicatedStorage")
local module = replicated.ModuleScript
module = require(module)
task.wait(3)
print(module.constant) --3.14 etc... (client change did not replicate to the server).

image

Now let’s reverse the roles and perform the change on the server.

--MODULE

local module = {}
module.constant = math.pi
return module
--LOCAL

local replicated = game:GetService("ReplicatedStorage")
local module = replicated:WaitForChild("ModuleScript")
module = require(module)
print(module.constant) --3.14 etc... (server change did not replicate to the client).
--SERVER

local replicated = game:GetService("ReplicatedStorage")
local module = replicated.ModuleScript
module = require(module)
module.constant = "Hello world!"
print(module.constant) --Hello world! (explicit server change replicated to the server).

image

The solution is to perform the ModuleScript modification on both the server and the client.