Car Driving System Changed - Won't work anymore

Hello Scripters,

I wanted to convert a script, which i would have needed to paste in many other cars, in just one script using the CollectionService. The code has worked before i converted it. However now it wont work anymore. (It makes the car drive)

There are no errors nor warnings. I can sit and see that i throttle with the driverSeat, but i’m just not moving. And i dont really know the cause, so i cannot fix it.

Here you can see the code.

local function handleCar(car)

	local chassis = car:FindFirstChild("Chassis")
	local driveSeat = car:FindFirstChild("DriveSeat")
	local passengerSeat = car:FindFirstChild("PassengerSeat")
	local sound = chassis and chassis:FindFirstChild("EngineSound")

	local leftMotor = car:FindFirstChild("LeftMotor")
	local rightMotor = car:FindFirstChild("RightMotor")
	local leftServo = car:FindFirstChild("LeftServo")
	local rightServo = car:FindFirstChild("RightServo")


	local wheelRL = car:FindFirstChild("Wheel_RL")
	local WHEEL_RADIUS = wheelRL and wheelRL.Size.Y * 0.5 

	local carName = car.Name
	local vehicleData = VehicleData[carName]

	if not vehicleData then
		warn("Vehicle data not found for car:", carName)
		return
	end

	local MAX_SPEED = vehicleData["MAX_SPEED"] / WHEEL_RADIUS
	local MAX_STEER = vehicleData["MAX_STEER"]

	local SOUND_MIN_SPEED = 0.6
	local SOUND_MAX_SPEED = 1.5
	local SOUND_MAX_VELOCITY = MAX_SPEED * WHEEL_RADIUS


	if sound then
		driveSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
			if driveSeat.Occupant then
				sound:Play()
			else
				sound:Stop()
			end
		end)

		RunService.Heartbeat:Connect(function()
			local speed = math.abs(chassis.CFrame.LookVector:Dot(chassis.AssemblyLinearVelocity))
			sound.PlaybackSpeed = SOUND_MIN_SPEED + (SOUND_MAX_SPEED - SOUND_MIN_SPEED) * math.min(speed / SOUND_MAX_VELOCITY, 1)
		end)
	end


	if leftMotor and rightMotor then
		driveSeat:GetPropertyChangedSignal("ThrottleFloat"):Connect(function()
			local angularVelocity = driveSeat.ThrottleFloat * MAX_SPEED
			leftMotor.AngularVelocity = angularVelocity
			rightMotor.AngularVelocity = angularVelocity
		end)
	end

	if leftServo and rightServo then
		driveSeat:GetPropertyChangedSignal("SteerFloat"):Connect(function()
			local targetAngle = MAX_STEER * driveSeat.SteerFloat
			leftServo.TargetAngle = targetAngle
			rightServo.TargetAngle = targetAngle
		end)
	end


	local function handleSeatOccupant(seat)
		seat:GetPropertyChangedSignal("Occupant"):Connect(function()
			if seat.Occupant then
				seat.LastCharacter.Value = seat.Occupant.Parent
				if not seat:GetAttribute(IS_CHARACTER_VISIBLE_ATTRIBUTE_NAME) then
					CharacterUtils.makeInvisible(seat.LastCharacter.Value)
				end
			else
				if seat.LastCharacter.Value then
					CharacterUtils.makeVisible(seat.LastCharacter.Value)
				end
			end
		end)
	end

	if driveSeat then
		handleSeatOccupant(driveSeat)
	end

	if passengerSeat then
		handleSeatOccupant(passengerSeat)
	end
end


for _, car in ipairs(CollectionService:GetTagged("Car")) do
	handleCar(car)
end

(The VehicleData MAX_STEER is 20 and MAX_SPEED is 80). I do have all variables at the top, i just didnt think they were worth it adding.

Any help is appreciated.

1 Like

It’s fixed, i wasnt getting the VehicleData[Car.Name]

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.