Self Driving Car Help

Hey there! I would like to make a car that can drive itself and keep itself on the right side of the road for my upcoming game.

The methods I am trying and know of are unreliable and difficult to use in a proper game without bugs.


At first I tried tinkering with Neural Networks but there is little doccumentation on acatually making a car of it so I tried 2 solutions, using the .touched event to detect whether it is on a road and whether it needs to turn (which is unreliable because it would only work in perfect conditions) and I also tried a system that uses CFrame to try to steer to the nearest point but it is janky and eventually runs off course

Touched Method

local car = script.Parent
local base = car.Base
local seat = car.Base.VehicleSeat

local frontWheel = car.WheelFR.Wheel.Hub.WeldConstraint.Tire

local last

function turn(current, last)
	print("Last = ",last.Orientation)
	print("Current = ",current.Orientation)
	local difference = current.Orientation.Y - last.Orientation.Y
	print("Difference = ",difference)
	
	
	
end

frontWheel.Touched:Connect(function(hit)
	if hit.Parent == workspace.Road then
		seat.Throttle = 1
		if last then
			turn(hit, last)
		end
		if hit.Name == "Left" then
			seat.SteerFloat = -0.269067
		elseif hit.Name == "Right" then
			seat.SteerFloat = 0.269067
		else
			seat.SteerFloat = 0
		end
		last = hit
	else
		seat.Throttle = -2
	end	
end)

CFrame Method

local last
local cf

local car = script.Parent
local base = car.Base
local seat = base.VehicleSeat

local road = workspace.RoadMK2
local nodes = road.Nodes


-- Build Path

for i,v in pairs(nodes:GetChildren()) do
	v.Transparency = 1a
	v.CanCollide = false
	local attach = Instance.new("Attachment")
	attach.Parent = v
	if last then
		local rod = Instance.new("RodConstraint")
		rod.Attachment1 = v.Attachment
		rod.Attachment0 = last.Attachment
		rod.Visible = true
		rod.Parent = v
	end
	
	last = v
end

-- Run
last = nil
for i,v in pairs(nodes:GetChildren()) do

	local mag
	repeat
		mag = (seat.Position - v.Position).Magnitude
		seat.ThrottleFloat = 0.7
		--print(mag)
		if last then
			cf = CFrame.new(last.Position, v.Position)
			--print(cf)
			print(v.Attachment.WorldPosition.X)
			print(last.Attachment.WorldPosition.X)
			if v.Attachment.WorldPosition.X > last.Attachment.WorldPosition.X then
				seat.SteerFloat = math.abs(cf.Y)*-0.35
			else
				seat.SteerFloat = cf.Y*0.35
			end
		end
		
		wait()
	until mag > 2 and mag < 7 or mag > 23
	
	
	seat.Steer = 0	
	seat.Throttle = 0	
	--print(mag)

	last = v
end

Not sure how I can do this. I really don’t want to use tweens as it would look un-natural. I want to make something on the fly but I just don’t know how.

If someone could give me some tips it would be greatly appreciated

Have you tried Neural Networks? Maybe that’ll help: Neural Network Library 2.0

I’ve got the library, I just don’t know how to edit it to drive a car rather than produce what I assume to be random numbers. I did try but found it too challenging. I would be more than happy to use them though but I just can’t get them to work

Since this is not a solved topic, I would like to suggest something

I’ve made one similar car, and I’ve tried several times
If your going for an automated plane, which does not touch ground, you should go for neutral networks
Just because its an AI

But if your going for a car which goes on road, you can use Roblox path findin service, however, this might not work well, but it atleast turns and goes on roads

Here’s my self driving car so far