Optimizing Code (Chat GBT vs My code)

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?

Modules save their state so you can just store all the classes in a local dictionary and have a static function called Get which returns the class with corresponding model.

What do you mean by that? That confused me.

local TrainMap = {}

local Train = {}
Train.__index = Train

function Train.new(model)
    local self = setmetatable({...}, Train)

    TrainMap[model] = self

    return self
end

function Train.Get(model)
    return TrainMap[model]
end

The … was underlined red inside of roblox studio. What do I change?

P.s would using metatables be the best way to code the vehicle system?

Send screenshot, also

It’s all personal preference

Would you say its the most optimized way? My personal preference is more what other people would suggest for me to do.

Heres the screenshot.
image

Hovering over the … says cannot use “…” outside of a vararg function

I’ve managed to fix it.

Here’s the code
–Module

local TrainMap = {}

local Train = {}
Train.__index = Train

function Train.new(model)
	local self = setmetatable({}, Train)

	TrainMap[model] = self
	self.Thing = "OOF"

	return self
end

function Train:Get(Model)
	print(#TrainMap)
	
	if not TrainMap[Model] then
		print("New")
		return Train.new(Model)
	elseif TrainMap[Model] then
		print("Find")
		
		return TrainMap[Model]
	end
end

return Train

–Server script

local Module = require(script.ModuleScript)

--print(Module.new("123").Thing)
print(Module:Get("123").Thing)

Module:Get("123").Thing = "Lol"
print(Module:Get("123").Thing)

print(Module:Get("123").Thing)

Module:Get("123").Thing = "Lol"
print(Module:Get("123").Thing)

The server script just shows how I call it and its just firing random things to test and it works ok. Would using metatables be the most performant way to create a vehicle system or another way?

mb I shud have left it as blank but the … was just a placeholder for the properties of the class.

I would use OOP because I’m more familiar with it and tbh it’s pretty good way to go about this. As for optimization, I don’t recall OOP having any performance overhead and I have seen many top devs use the OOP pattern so it should be just fine. But just like I said before, don’t sacrifice readability over optimization in such cases. Using OOP or generic tables here would essentially just harbor the same results with little difference in performance which half of the time won’t even be noticeable