OOP Object on Client and Server

So I’m coding with an OOP mindset and I just had a question. Say I have an AI on the server. There’s a physical representation in the DataModel. However, some “methods” I want to have it be called on the server, like FindPath. Other methods I want to call on the client for things such as visual effects.

What’s the conventional way to go about this? Should I make an object on the server and client or the same AI with different methods respectively?

Or should I have a utility class with static methods on the client to do the effects, passing the AI physical representation through parameters. (Current way I’m doing it)

I’m kind of stuck. Is it fine to have my own OOP object on the client and server for the same “thing”??

1 Like

Personally, I would recommend having methods that are clearly for the client or clearly for the server as separate classes with a communication method like RemoteEvents between them. The main reason I would recommend separate classes is you may get confused during development if a method should be used on the client or server. If your OOP method allows for inheritance, you can have a main class (like AI) and separate classes for the client and server that extend it (like ClientAI and ServerAI).
Additionally, keeping them separate would improve security slightly since the code isn’t exposed to the client, although this is very minor and would only just protect things like how an NPC splits rewards or cycles through attacks.

2 Likes

To add @TheNexusAvenger point, Quenty’s Nevermore engine is a pretty good example of splitting up the codebase in a clean and organized way. It uses Client, Server and Shared classes/databases. Highly recommend at-least looking into this. GitHub - Quenty/NevermoreEngine: ModuleScript loader with reusable and easy unified server-client modules for faster game development on Roblox

1 Like

Me personally, I would create a class and use RunService.IsServer and check to see what methods to use for each.

Would you do something similar for client and server objects of the same weapon?

That way you can have the server weapon object, although the model is on the client, record to actual amount of ammo and do logic checks to prevent cheating?

I just follow what egomoose does with his placement system. He handles client and server interactions very well.

Can you link me please? Is it open sourced?

Nexus’s recommendation makes sense for what you’re trying to go for with your weapon case. Your main class can be Gun which has child classes GunClient and GunServer. GunServer can handle your server weapon object and any methods associated with the server, such as welding, while GunClient handles the client model and creating visuals. Your cheating checks can be done in GunServer . There may be behaviors, like reloading, where you’d have the client and server involved. The client method (apart of the GunClient class) plays the animation and any effects, while the GunServer class method will validate the actual reload. Then you’d need to properly communicate with events.

Yeah, it’s a community tutorial.