Self-Driving Car Help

Hello, It’s me again.
I am trying to make a car that drives itself for my upcoming game that needs to follow the roads, interact with players and stop at traffic lights.

All the methods I have tried either
A - Go off course after a bit and get lost
B - Is fixed so all turnings have to be the same size and ends up driving off the map
C - Can’t navigate to it’s marker
D - Don’t move in the first place

(NOTE : All of this work here is only to follow the road)

I have tried to use .Touched to see if it is on a road and whether it needs to turn which lead to result B.
I also tried getting it to run through a series of nodes and either turn left or right a bit to to stay on track which eventually runs off course and gets too far away from the node for the script to mark the destination as reached (Result A) I tried another system more the machine learning side where if it hit a wall, it would leave a marker there and respawn but the issue was that cars don’t have built-in pathfinding and I wasn’t able to produce pathfinding so it couldn’t navigate to it’s point (Result C). Finally as a last resort I tried tweening it (which I really don’t want to do as it doesn’t look real) but that didn’t move at all (Result D) (I would not like help with tweening, I don’t think it is practical for this use)

My code:

Node tracking:

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.3
			else
				seat.SteerFloat = cf.Y*0.3
			end
		end
		
		wait()
	until mag > 2 and mag < 7 or mag > 20
	
	
	--seat.Steer = 0	
	seat.ThrottleFloat = -0.3
	repeat wait() until mag > 2 and mag < 7
	seat.Throttle = 0
	--print(mag)

	last = v
end

Machine Learning:

script.Parent.Hitter.Touched:Connect(function(hit)
	if hit.Parent ~= workspace.RoadNodes then
		seat.Throttle = -0.2
		wait(1)
		seat.Throttle = 0
		table.insert(points, {script.Parent.NodeCreator.Position.X, script.Parent.NodeCreator.Position.Z})
		
		
	end
end)

Check to see if it’s on road:

frontWheel.Touched:Connect(function(hit)
	if hit.Parent == workspace.Road then
		seat.Throttle = 1
		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)

Tweening:

for i,v in pairs(nodes:GetChildren()) do
	print(i)
	if last then
		cf = CFrame.new(last.Position, v.Position)
		local distance =(v.Position - seat.Position).Magnitude
		print(distance)
		tweenService:Create(base, TweenInfo.new(distance,Enum.EasingStyle.Linear), {CFrame = v.CFrame}):Play()
		local cr = coroutine.create(function()
			print(base.Position)
		end)
		coroutine.resume(cr)
		wait(distance/2)
	end
	last = v
end

I’ve just about had it with these cars. If anyone could help me with Self-Driving cars it would be very much appreciated. I’ll say it again down here, I’d rather not use the tweening method.

Thanks.

2 Likes

I just skimmed through your post, and might be totally off here, but why not add a humanoid to the car and do :MoveTo(), this way, you can control where it goes, and simply run it through a specified loop over and over again. You could code in “Stops”, or places where the car stops to pick up players, like a bus stop. You can also have it pause at intersections. For more information, read this article: Character Pathfinding | Documentation - Roblox Creator Hub

OR don’t even include a Humanoid Model | Documentation - Roblox Creator Hub

Hope this helps, just an idea

4 Likes

I haven’t done this as the animations would look un-realistic as the wheels spin and turn when the vehicle steers. Thanks though!

Just a thought, most Robloxians wouldn’t be bothered if it looks slightly fake (after all, it’s Roblox), best of luck!

The humanoid won’t move if the car is inside it. So moveTo doesn’t work. Thanks though.

1 Like

Put the humanoid inside the model, unanchor everything in the car, and weld the pieces together. Then, set all of the unessential car pieces to massless (so the car isn’t too heavy to move), except for the basepart. Make sure to set the Primary part of the model. This should move, but if it doesn’t, :MoveTo() probably timed out (if it doesn’t reach its mark in x seconds, it stops). You might have to repeatedly call it, or just scrap the MoveTo entirely. Good luck!

1 Like

image so would my car look like that or should the npc be an entire npc

In theory, that should be fine, as long as you set the Model Primary part to the Base(of course, that’s just in theory)

That works, Thanks. But now the issue is making sure the humanoid has reached its destination before it starts to walk again

Use this:

humanoid:MoveTo(pointA.PrimaryPart.Position) -- makes it move
humanoid.MoveToFinished:Wait() -- waits until it reaches the position

From the article: Character Pathfinding | Documentation - Roblox Creator Hub

1 Like

Thank you so much! Never knew that was a function!

Happy to help, I’ve ran into the same problem (but less complex) with my game ELIMINATE - Skill Test - Roblox

If one of the replies is the answer, I would mark it, so future people can easily find the answer. Good luck with your game!

I’ve marked the top one so others can read down the thread.

Great thanks, excited to see your game!

1 Like

What my friend did to make this concept realistic was use a humanoid and your cars API.

whatever direction the Humanoid is traveling, turn the wheels and make it go when its walking. This will make the car move more realistic.

But you will need to make your own script idea if it get hit off road by another vehicle.