Everything seems to work, but the script is not working

Hello, I have a script, but this script prints everything except for “No right” and “No left”. This script doesn’t have any errors or warnings, but it doesn’t work. What is causing the script to not work? Thank you.

I posted many scripting support topic relating to this script because this script is keep on not working.

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

print("Variables work")

spawn(function ()
	-- Makes the car move forward
	while wait(15) do
		while wait() do
			car:PivotTo(car:GetPivot() * CFrame.new(0, 0, -0.1))
			print("Car moves forward")
		end
	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
	print("this works1")
	right = true
else
	right = false
end

if table.find(touchingLeftSensor, road) then
	print("this works2")
	left = true
else
	left = false
end

spawn(function ()
	while wait() do
		print("wait works")
		while true and wait() do
			print("works works")
			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
end)

Also, please don’t copy the script and use it for your game.

1 Like

The two variables are either both true or both false, my guess is that they are both false and there may be something wrong with how you are trying to detect and change true/false also this:

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

if table.find(touchingLeftSensor, road) then
	print("this works2")
	left = true
else
	left = false
end

only runs once so idk what you’re trying to do here

2 Likes

Oh, so do you mean I have to put a while wait() loop? Also, how this works is, the road gets put inside the table, and if there is “road” in the table, then it will turn right / left variable to true. If it’s not in the table, it will turn the right / left variable to false.