Hello, I’ve written some code for a train system and I’ve copied it into Chat GBT and asked it to make the code more performant and optimized but, I’m wondering if anyone here on the Devforum would know what code is more performant and optimized, my code or Chat GBTs code.
If anyone could tell me which code is more performant and optimized then please let me know! If anyone would like to re write it into a better version then that’ll also be good! Any other tips and tricks are appreciated!
Does anyone have any suggestions to writing optimized code?
My Code
local Trains = {}
function SetOrder(Train)
local Cars = Train:GetChildren()
table.sort(Cars, function(A, B)
return A.PrimaryPart.Position.X > B.PrimaryPart.Position.X
end)
for Index, Car in Cars do
Car.Name = Index
end
end
return function(Model)
if not Trains[Model] then
local Train = {}
Train.Unit = nil
Train.CardinalDirection = nil
Train.Connections = {}
function Train:DeleteTrain()
Model:Destroy()
table.clear(Train)
end
function Train.Couple(North, South)
if North:GetAttribute("Coupled") == false and South:GetAttribute("Coupled") == false then
local NewTrain = Instance.new("Model", workspace.Trains)
local Train1 = North.Parent.Parent.Parent.Parent:GetChildren()
local Train2 = South.Parent.Parent.Parent.Parent:GetChildren()
North:SetAttribute("Coupled", true)
South:SetAttribute("Coupled", true)
Trains[NewTrain] = Train
table.clear(Trains[Train1])
table.clear(Trains[Train2])
for Index, Car in table.move(Train1, 1, #Train1, #Train2 + 1, Train2) do
Car.Parent = NewTrain
end
end
end
function Train.CouplerTouched(Coupler)
Train.Connections[Coupler] = Coupler.Touched:Connect(function(Hit)
if Hit.Parent.Name == "Couplers" then
if Hit.Name == "North" and Coupler.Name == "South" then
Train.Couple(Hit, Coupler)
elseif Hit.Name == "South" and Coupler.Name == "North" then
Train.Couple(Coupler, Hit)
end
if Train.Connections[Coupler] then
Train.Connections[Coupler]:Disconnect()
end
if Train.Connections[Hit] then
Train.Connections[Hit]:Disconnect()
end
end
end)
end
function Train.SetUpCouplers()
for Index, Unit in Model:GetChildren() do
local NorthCoupled = Unit.Miscellaneous.Couplers.North:GetAttribute("Coupled")
local SouthCoupled = Unit.Miscellaneous.Couplers.South:GetAttribute("Coupled")
if NorthCoupled == false and SouthCoupled == true then
Train.CouplerTouched(Unit.Miscellaneous.Couplers.North)
elseif NorthCoupled == true and SouthCoupled == false then
Train.CouplerTouched(Unit.Miscellaneous.Couplers.South)
elseif NorthCoupled == false and SouthCoupled == false then
Train.CouplerTouched(Unit.Miscellaneous.Couplers.North)
Train.CouplerTouched(Unit.Miscellaneous.Couplers.South)
end
end
end
function Train.DriveMode(Mode)
Mode = (Mode == 1 and "Forward") or (Mode == 2 and "Off") or (Mode == 3 and "Reverse") or (Mode == 4 and "Isolate")
Train.Unit:SetAttribute("DriveMode_" .. Train.CardinalDirection, Mode)
end
function Train.UpdateStuff(Unit, CardinalDirection)
Train.Unit = Unit
Train.CardinalDirection = CardinalDirection
end
Trains[Model] = Train
return Train
elseif Trains[Model] then
return Trains[Model]
end
end
Chat GBTs Code
local Trains = {}
local function CoupleTrains(North, South)
local NewTrain = Instance.new("Model", workspace.Trains)
local Train1 = North.Parent.Parent.Parent.Parent:GetChildren()
local Train2 = South.Parent.Parent.Parent.Parent:GetChildren()
for Index, Car in ipairs(Train1) do
Car.Parent = NewTrain
end
North:SetAttribute("Coupled", true)
South:SetAttribute("Coupled", true)
Trains[NewTrain] = {}
end
local function CouplerTouched(Coupler, Train)
Train.Connections[Coupler] = Coupler.Touched:Connect(function(Hit)
if Hit.Parent.Name == "Couplers" then
if Hit.Name ~= Coupler.Name then
CoupleTrains(Hit, Coupler)
end
if Train.Connections[Coupler] then
Train.Connections[Coupler]:Disconnect()
end
if Train.Connections[Hit] then
Train.Connections[Hit]:Disconnect()
end
end
end)
end
local function SetUpCouplers(Train)
for _, Unit in ipairs(Train:GetChildren()) do
local NorthCoupled = Unit.Miscellaneous.Couplers.North:GetAttribute("Coupled")
local SouthCoupled = Unit.Miscellaneous.Couplers.South:GetAttribute("Coupled")
if not NorthCoupled and SouthCoupled then
CouplerTouched(Unit.Miscellaneous.Couplers.North, Train)
elseif NorthCoupled and not SouthCoupled then
CouplerTouched(Unit.Miscellaneous.Couplers.South, Train)
elseif not NorthCoupled and not SouthCoupled then
CouplerTouched(Unit.Miscellaneous.Couplers.North, Train)
CouplerTouched(Unit.Miscellaneous.Couplers.South, Train)
end
end
end
local function DriveMode(Train, Mode)
local driveModes = { [1] = "Forward", [2] = "Off", [3] = "Reverse", [4] = "Isolate" }
local modeStr = driveModes[Mode] or "Off"
Train.Unit:SetAttribute("DriveMode_" .. Train.CardinalDirection, modeStr)
end
local function SetOrder(Train)
local Cars = Train:GetChildren()
table.sort(Cars, function(A, B)
return A.PrimaryPart.Position.X > B.PrimaryPart.Position.X
end)
for Index, Car in ipairs(Cars) do
Car.Name = tostring(Index)
end
end
return function(Model)
if not Trains[Model] then
local Train = {
Unit = nil,
CardinalDirection = nil,
Connections = {}
}
function Train:DeleteTrain()
Model:Destroy()
Trains[Model] = nil
end
Trains[Model] = Train
SetOrder(Model)
SetUpCouplers(Model)
return Train
else
return Trains[Model]
end
end
Thanks!