My script isn't working

Hello, I have a script, but it isn’t working. The script’s main problem is the sensor won’t sense the road. What am I doing wrong? Thank you.

This is the script:

-- Variables
local car = game.Workspace.SelfDrivingCar
local forwardSensor = car.Sensor1
local rightSensor = car.Sensor2
local leftSensor = car.Sensor3
local road = game.Workspace.Road
local player = game.Workspace.HumanoidSensor

local rightSensorTable = {}
local leftSensorTable = {}
local touchingRightSensor = rightSensor:GetTouchingParts()
local touchingLeftSensor = leftSensor:GetTouchingParts()

local forward
local right
local left


-- Makes the car move forward
while wait(15) do
	while wait() do
		car:PivotTo(car:GetPivot() * CFrame.new(0, 0, -0.1))
	end
end

-- Checks if the road is touching the sensor.
for i, v in pairs(touchingRightSensor) do
	print(v)
	table.insert(rightSensorTable, v)
end

for i, v in pairs(touchingLeftSensor) do
	print(v)
	table.insert(leftSensorTable, v)
end

if table.find(touchingRightSensor, road) then
	right = true
else
	right = false
end

if table.find(touchingLeftSensor, road) then
	left = true
else
	left = false
end
	
while wait() do
	while true and wait() do
		if right and not left then
			print("No left.")
			local turnCar = CFrame.Angles(0, -5, 0)

			local drivingCar = car:GetPivot()

			car:PivotTo(drivingCar * turnCar)
		end

		if left and not right then
			print("No right.")
			local turnCar = CFrame.Angles(0, 5, 0)

			local drivingCar = car:GetPivot()

			car:PivotTo(drivingCar * turnCar)
		end
	end

end

1 Like

You should either put the while wait()s in a spawn or put them in a function and run that function, as when the script gets to the while wait() it doesn’t keep going past it. (If that doesn’t make any sense tell me.)
Basically do this:

spawn(function()
-- Makes the car move forward
while wait(15) do
	while wait() do
		car:PivotTo(car:GetPivot() * CFrame.new(0, 0, -0.1))
	end
end
end

and the same with the other while true.

Also, if you want the car to move every 15 seconds then remove the “while wait() do” part inside of it, if you want it to constantly move, remove the “while wait(15)” part

2 Likes

Thanks, for the while wait(15), I wanted the car to start moving after 15 seconds, so when the player joins, the car doesn’t move right away.