Client/Server Vehicle system with OOP?

I recently decided to refactor my helicopter controller into a OOP class to make it more modular. The problem is sending the helicopter table into the client so that it can use its control functions when the client is the current driver. the table, created in a server, doesnt replicate into the client, and trying to send it through a RemoteEvent causes any functions stored to be lost.

How do i make it so that the helicopter object replicates in the client, and allows it to use some of its functions.

it is not possible to send functions over a remote event/function. u should create a separate module script with the necessary functions in replicated storage so it can be replicated to both server and client

1 Like

You need to define a client side version of your control module that is located in a place that the client can access in the DataModel. That, or use a module that both the client and server can share (but be warned that exploiters can see this code once required on the client).

Once you’ve done that, you can set up some framework to replicate the state of vehicles. A “state” is just any collection of data, i.e., a table that stores certain attributes about the vehicle, such as health, fuel, headlights on/off, etc. This is the hard part. You need to make sure that clients that join an in-progress game are updated on current vehicle states, among many other edge cases. If you don’t feel like custom-making a system to handle this for you, there are modules out there that can do it for you, like loleris’s ReplicaService. Since you want a Lua vehicle object on both the client and the server, you can simply replicate some of the data from the server’s version of the object. You don’t need to replicate everything, just the stuff that the client needs. Always abide by the general rule of thumb that if the client doesn’t immediately need it, it shouldn’t be replicated. And also don’t waste network throughput on it.

After that though, it’s pretty smooth sailing. On the client, all you need to do is use your constructor to create the client-side version of the vehicle “object” whenever a new vehicle state is replicated. You can then respond to changes in the state to render things as needed or run logic. I assume that you want the server to be the source of truth for a vehicle state (as you should), so make sure that the server is the one creating and ultimately managing vehicle states. As an aside, you probably don’t actually need a Lua object on both client and server.

I will say that a networking solution like ReplicaService makes making a game 10x easier. I custom-made my own lightweight version that fits my needs without the stuff that I don’t need, but the general idea is the same. Strictly speaking, you do not need a module or discrete system for this. You can easily get away with the classic setup of a few RemoteEvents for specific purposes and call it a day. But a dedicated networking setup for shared states does make things easier.

1 Like

Thanks, this explination helped. I ended up creating 2 seperate module script, a server one in ServerScriptService and another in ReplicatedStorage that only contains functions for controlling using input

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