Script Efficiency

Hey there,

I was currently creating a Golfcart and after running into a few issues with efficiency as this is my first vehicle, I wanted to ask if anyone had more efficient solutions to do what I’m trying to do with my code and the issues I am facing.

Firstly, I detect when a player sits on a seat and then I check for key inputs which leads to firing a remote event to create a movement for the cart on the server. The server code I have looks something like this:

Remote.OnServerEvent:Connect(function(Player, Model, Direction)
	if Direction == "Forward" then
		for Key,Object in pairs(Model:GetChildren()) do
			if Object:IsA("Model") and Object.Name ~= "Tires" then
				for Index,Part in pairs(Object:GetChildren()) do
					if Part:IsA("MeshPart") then
						Part.CFrame = Part.CFrame * CFrame.new(-.5, 0, 0)
					elseif Part:IsA("Seat") then
						Part.CFrame = Part.CFrame * CFrame.new(0, 0, -.5)
					end
				end
			elseif Object.Name == "Tires" then
				for k,x in pairs(Object:GetChildren()) do
					for i,v in pairs(x:GetChildren()) do
						if v:IsA("MeshPart") then
							v.CFrame = v.CFrame * CFrame.new(-.5, 0, 0)
						end
					end
				end
			end
		end
		-- RotateTires("Forward", Model)

The ReotateTires functions looks like this:

local RotateTires = function(Direction, Model)
	if Direction == "Left" then
		local Vector = Model.Tires.FrontLeft.Outside.Orientation.Y
		if Vector < 30 and Vector < 30 then
			Model.Tires.FrontLeft.Outside.Orientation += Vector3.new(0, 1, 0)
			Model.Tires.FrontRight.Outside.Orientation += Vector3.new(0, 1, 0)
			Model.Tires.FrontLeft.Rim.Orientation += Vector3.new(0, 1, 0)
			Model.Tires.FrontRight.Rim.Orientation += Vector3.new(0, 1, 0)
			Model.Tires.FrontLeft.Inside.Orientation += Vector3.new(0, 1, 0)
			Model.Tires.FrontRight.Inside.Orientation += Vector3.new(0, 1, 0)
		end
	elseif Direction == "Right" then
		local Vector = Model.Tires.FrontLeft.Outside.Orientation.Y
		if Vector > -30 and Vector > -30 then
			Model.Tires.FrontLeft.Outside.Orientation -= Vector3.new(0, 1, 0)
			Model.Tires.FrontLeft.Rim.Orientation -= Vector3.new(0, 1, 0)
			Model.Tires.FrontLeft.Inside.Orientation -= Vector3.new(0, 1, 0)
		end
	elseif Direction == "Forward" then
		Model.Tires.FrontLeft.Outside.Orientation += Vector3.new(0, 0, 5)
		Model.Tires.FrontLeft.Rim.Orientation += Vector3.new(0, 0, 5)
		Model.Tires.FrontRight.Inside.Orientation += Vector3.new(0, 0, 5)
	end
end

I change directions via this code below:

Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame() * CFrame.fromEulerAnglesXYZ(0, .02, 0))

My main question is if I can do something better than edit CFrames for all the parts. You might say, a primary part, however, I’m having several welding issues and whatnot this way. This code does function but I do feel that I can do better cleaning up memory storage and improving on some of this. Please feel free to list suggestions and comments below.

Thanks,
Iukyns