Optimizing Code (Chat GBT vs My code)

I thought that is why you are here lol.

Honestly, I would just trust my own instinct and skill rather than some AI, as the latter can be extremely flawed at times and the worst thing is you can’t even tell if they are right or wrong. And don’t get me wrong, I use ChatGPT for gathering ideas and input but never for optimizing/getting code because every time I did so, I ended up with poorly optimized/thought out scripts

1 Like

Your variables were a bit confusing, I’m guessing “North” and “South” are a north train and a south train right? With this change, the usage would be something like Train:Couple(South).

The point of objects is to store pointers with the logic that operates them. So if you had two trains, you would make two new “Train” objects from this module. They would share the same function references, and have their own members such as “Unit” or “CardinalDirection”.

For example:

local trains = {}

local trainA = Train.new()
local trainB = Train.new()

trains.A = trainA
trains.B = trainB

For some other methods: Train.CouplerTouched(Coupler) would be trainMethods.CouplerTouched(Train, Coupler). Or Train.DriveMode(mode) would be trainMembers.DriveMode(Train, mode). In my code I used the self variable before, but for these examples I changed it to Train

The functions should be little faster in the ChatGPT script, but it looks wrong.

Unlike what some novice programmers tell you in these comments, you don’t need to save 7 bytes and 0.0000063 seconds by making your script very ugly and hard to work with.

1 Like

Upvote, don’t sacrifice readability over micro optimization

1 Like

To be honest, I agree, I’ve used ChatGBT to help me fix a couple things or where to start on a couple things and a lot of the time it doesn’t work properly lol, most of the time I have to ask chat GBT a thousand questions before somethings fixed. Pretty sure GPT 3.5 is getting a bit outdated and GBT 4 is a newer version so it might work better but I’m not going to waste money on something that “could” work good on a free version. Normally I’d only use chat GBT on something I can’t figure out on my own.

I’ll agree on this but I’m not really sure what effect 0.0000063 seconds is going to do but I do assume it’ll pretty much look the same every time.

Let me give you a hint:

Unless you are creating billions and deleting billions of objects every frame (Which isn’t even possible in roblox)

It’ll have no difference, just look way uglier and harder to work with.

Ok one unit is this. One car is half a unit so one car is named South and one car is named North and these reference the end so south end and north end these can’t be called front and back because depending what way you drive the train from the front and back side will change and it makes it easier to tell.

Also one unit would be one train and 2 units coupled would be 1 train and 4 units coupled would be 1 train but 2 units uncoupled would be 2 separate trains

So basically South and North aren’t two trains going two different directions they are just the 2 ends of 2 units.

When the train couples it gets the coupler from the south side of one unit and it gets the coupler from the north side of the other unit.

I’ve added some markings so you know whats what


image

I was thinking more of when the train updates it’ll parent all the units from both trains and convert it into one train and delete the 2 original train models, reason for this is its easier to handle headlights, taillights, doors, and whatever else.

Let me know if this clears things up for you!

Oh alright thank you!

I’ll still look for improvements and a better way and see what other people say.

Would I be better off to use a normal table like that or would metatables be better?

Speaking of which, both methods rely on Attributes, which, although they are significantly faster than value objects, are still poor compared to OOP’s use of metatables.

So do you think I should be using the way he is using but with metatables or how do you think I should write the code?

I would use OOP here, with a train or carriage class in the ModuleScript, so yes, using metatables would be best. Using colon methods to automatically pass the self parameter, you can couple up carriages, etc.
Here’s a tutorial if you’re stuck:

1 Like

I think it’s worth pointing out why attributes aren’t optimal because they get replicated to all clients and hence OP is just wasting resources doing that when no client really need to know whether the train is coupled or not. Btw good call, I didn’t catch that.

1 Like

I’ve read it.

How could I save the newCar table for later?
Do I have another table for Vehicles and then add the newCar table into that or can I find the newCar table from the metatable that it saves to?

I want to be able to find it like Vehicles[VehicleModel] and then be able to change things from that. I would like to create the class for the vehicle when the vehicle is spawned not when the player enters the seat.

I’ve probably made this reply very confusing but let me know if you need me to add more details for what I mean!

The attributes there because the server needs to know weather the couplers are already coupled or not so it doesn’t couple again.

Yes, the key point being server, the clients have no business in knowing about it but using attributes replicates the state to every client, since the train will be parented to workspace. Attributes aren’t as bad as ValueBases as @ValtryekRBLX pointed out, but there really isn’t a reason to use it here as you can just create a property called IsCoupled which will reflect the state

1 Like

Ok.

This train system is really just one I’m remastering from my original one… my original one was probably a bit silly the way I did it though lol.

Would you suggest using the meta tables way though or would you suggest a different way?

How would I get a reference to the newCar table?

Do I save the newCar table to a normal table or can I find it from using metatable?
And what is the index of the metatable its in?

So basically how to find the newCar table from the trains Model?