Improving of train system

Hello, I’m working on a train system and I’m wanting to see if I can somebody to perhaps take a look into the code of it? I’d like to improve readability, performance and whatever else.

The way the code is setup on the server is, theres 3 modules, one for the general train, one for each dual carriage set and one for the doors.

For the client, theres 1 module that fires remote events to these server modules when a player wants to turn on the end lights and other things.

Its kind of hard to explain and seperate the different codes into their own things and I’d prefer to give a basic place file to anyone who seems trust worthy, via dms

Thanks!

Is this a riddle? I love riddles! I am going to try to solve it and find the code!

// Sarcasm, sorry. Please list the code so we can give our feedback on it.

Its kind of hard of which code to pick out but heres a place file.

The modules we’re focusing on are:
ServerStorage.CustomServices.TrainService
ServerStorage.CustomServices.MultipleUnitService
ServerStorage.CustomServices.DoorsService
ReplicatedStorage.CustomServices.TrainService

Some functions may be fired by one of the other services, by the client or by another function in that same service, which is why its hard to pick which functions to pick out.

PlaceFile Removed.

I’m not taking a complete look at your code, but here are a couple things I found:
afbeelding
Connections is a module-wide variable, meaning each time a train will be driven, this connection will be overwritten. Maybe add a connections variable to the train data structure?


This is dangerous. You’re allowing any client to send unfiltered data to the whitelisted functions, which could wreak havoc.

I get the feeling ot seeing this style of code a lot in your game. This function looks like you’re combining the GUI display, the appearance of the train, and the control of driving the train in a single function.

Moving all the gui functionality to another function makes this a lot easier to read, as I now immediately know what the function does, and don’t need to spend time figuring it out.

Where does the -110 come from? Is this some known global constant? If so, define the constant in an actual module, and retrieve it from there. If you ever want to change this, you’ll have to look through each occurance of -110, and change it accordingly, which is time consuming and error prone.

You can also put a decent chunk of the DriveLoop code into another function. This way, I know I can instantly ignore the GetNewCurrentSpeed function unless I know I need to change something relating to the speed.
Is this module used on the server or the client btw? I wouldn’t expect the client to be responsible for setting the velocity of a linevelocity, as the server should be the one to determine the actual speed of the client. I’d expect the server to set the speed of the linevelocity instead, and tell the client the actual position. The client should then rubberband to be closer to the actual position.

I’ll reply again later if I need to because I’m typing from my iPad but,

There’s some code that disconnects and clears the connection from the table when the driver exits the seat (although this will change, because the driver of the train will depend on who’s first to turn the key)

How should I do the remote events instead?

The -100 is an extra position on the throttle gui, which is another way the emergency brake can be activated. That script can be found in the screenGUI under throttle as local script, the drive mode one is just to handle the Forward and Reverse lever.

That module is handled on the client, the train is player driven not server driven btw. If you’d like to give it a test play to see how things work then I’d be more than happy to give you a quick tutorial.

How is my DoorsService module?
There’s a couple more modules that may need to be looked at like,
ReplicatedStorage.CustomServices.TrainService.DriversConsole
ReplicatedStorage.CustomServices.PIDs (Passenger Information Displays, not yet finished and only visible to the client who sets it, when I want to make it server sided at some point)

I need to have a big code clean up because it’s currently hard to,
Get how many doors are closed and have the right of way function only work when all doors are closed and locked (controlling door only needs to be closed and right of way would lock this when pressed)
A bit hard to make the PID system server sided right now.

There’s some code that disconnects and clears the connection from the table when the driver exits the seat (although this will change, because the driver of the train will depend on who’s first to turn the key)

I’ve seen this part of the code, yeah. This still makes the code reliant on which order you call the functions in, and only permits a single player to drive a train at any given time. Not that bad if it’s called on the client, but still error-prone if you happen to call the drive and leave functions out of order.

How should I do the remote events instead?

You ought to ensure that the player input gets validated in some way.
In my current project I create a remote event for all events (building, claiming a plot, etc.). I’ve also created a small module that binds a remote event to 1 or more functions, based on which types the parameters are. Any call with invalid types is immediately discarded. After the bound function with the relevant parameters gets found and called, I make sure to add some verification logic to ensure that the player is allowed to execute the current action.
For your game this might be overkill, or you might need more automatic verification. You could add an automatic check that the given player is the owner of any passed train value, for instance.

The -100 is an extra position on the throttle gui, which is another way the emergency brake can be activated. That script can be found in the screenGUI under throttle as local script, the drive mode one is just to handle the Forward and Reverse lever.

It’s currently a magic number you might not know the use of if you look at the code for a first time. If you’re developing this solo you’ll be fine leaving this in, but if you intent on working together with other programmers later on it’s good practice to define this value in a single location. This’ll also save you lots of trouble if you need to modify this number sometime later.

That module is handled on the client, the train is player driven not server driven btw.

If the train system is competitive, I recommend changing this to be handled on the server to prevent exploiters. If it’s just a casual train system, this will once again be fine to keep as it is.

If you’d like to give it a test play to see how things work then I’d be more than happy to give you a quick tutorial.

Sure I’d be up to play this sometime. Maybe this weekend, on a saturday?

How is my DoorsService module?
There’s a couple more modules that may need to be looked at like,
ReplicatedStorage.CustomServices.TrainService.DriversConsole
ReplicatedStorage.CustomServices.PIDs (Passenger Information Displays, not yet finished and only visible to the client who sets it, when I want to make it server sided at some point)

I’m afraid I don’t have enough time to take an in-depth look at your entire project.

So, any player can spawn a train any where anytime and any player could sit in any of the drivers seats but they wouldn’t have control of the train until they turn the master key to the ON position and this is when a remote event would turn on the end light of that cab they’re driving from, set network ownership, check whether or not the last driver had the emergency brake and park brakes released or not, penalty brake and train stop (written as trip cock as real life in the script but train stop for players for obvious reasons) statuses, check whether or not that cab is coupled or not and some other things.
What do you mean by leave the functions out of order?

Doesn’t this reduce performance, network traffic or something?
So should I create a function on the server that check the required parameter types with the given and allowed and then continue if its ok and perhaps kick the player if the parameter types aren’t allowed?

Be good if it was impossible to exploit so us developers don’t have to go through hours of exploit prevention lol.

Its a bit complex lol, so its based off of the trains real life in my country and I’m trying to make all the controls and things be close to same, so it is a super realistic system with currently less than 4000? lines of code which might be good?

Lol your saturday or my saturday? :sweat_smile: My timezone is NZST and I’m a bit busy on saturday during the day but would be avalible during the evening/night, judging by your devforum profile saying netherlands, I believe you’re 12 hours behind me which makes things harder :grimacing:. FYI, currently I got 3 versions I’m working with, the new one (which is the one I’ve given), my old one with a weird testing map and some other smaller features I haven’t put in my new one yet and the one I managed to import my countries real life train tracks curves and turns and other stuff into roblox, this uses the older UI and button stuff. I’ll put you into the new version that I’m working to improve if you do get time to play and if you’d like to give the other 2 ago aswell just for the fun of it then we can do that too!

Alright its ok, I can work off of your current and future suggestions and whatever else other people leave.

FYI, in the future I plan to add an ability for me and other trainers to have some form of control of the train (See drivers screen and other info and perhaps have the abiltiy for a couple of controls.) I may also add the ability for a player who sits in a drivers seat that doesn’t want to drive the train to see the drivers screen and perhaps have some control of the train if they wanted.

I think this is all, hopefully I didn’t miss anything.

Hi, does anybody else have any other suggestions that would improve performance, readabilty and whatever else that would improve the overall coding?

Final call, any other suggestions and ideas that would improve performance, readabilty and overall coding for it?