Optimizing Code (Chat GBT vs My code)

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

I’ve also seen a lot of developers use Metatables but I’ve never really used Metatables myself so its kind of new to be knowing normal tables and reading other developers code really does help you learn it though!

To be honest, I don’t really have a problem reading the code. I know the code I’ve provided probably isn’t the cleanest but I normally test things first before I go make it better to read because its a waste of time if it doesn’t work.

Just a question, when I used normal tables in the first code I’ve provided what would the difference be between that and using metatables?

Absolutely, I have done that myself countless times. However one thing to note is that most of the devs use metatable primarily for OOP but not usually for other hacky functionalities which is possible with it (except for a few features like lazy loading).

There is essentially no difference, except that you are creating properties + functions every time you create a new object (train) whereas in OOP you only create the properties (and sometimes call some function inside .new for initializing and such).

Also, if you are interested, another pattern might be the component/binder pattern. It’s not necessarily ECS, more like a mix of OOP and CollectionService tags to define behaviour for components. There are various modules which provide this behaviour namely Binder by Quenty and Component by sleitnick. Basically you tag one of your train models “Train” and the class would automatically instantiate a Train component/class for the model.

To be honest, its probably easier just to do it the way I sent in the most recent code. Probably easier to just forget about performance all together but then that could make the game annoying for people to play so I won’t do that.

As I said multiple times, the performance boosts earned are so small that players won’t even notice it. Rather than overoptimizing your code, you should instead try to optimize parts of your game which when optimized will give noticeable performance boosts such as physics, animation and so on.

Ok, alright thank you for your time today!

Anytime :wink:

1 Like

Oh, just realized that you’re the guy from this post that I made the other day lol

Damn, I didn’t even realize :joy: :joy: :joy:

Lol, I didn’t either but I did have a thought on I also thought you might of been the guy that helped me with another thing of the train but that wasn’t you. I normally recognize a lot of people around on the Devforum lol.

2 Likes