This part of the script doesn't work

Hello, I have a script, but I have figured out which part of the script does not work. The “wait works” print does not print out. I think this is causing the scripts inside the loop to not run. What can I do? Thank you.

This is the script:

spawn(function ()
	while wait() do
		print("wait works") -- This doesn't print out.
		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)

Try using coroutines! I’m always do that. Spawn is kinda deprecated.

1 Like

Coroutines? Can you show me an example? I’m not familiar to using them.

I’ve never used spawn before, just coroutines, but I believe if you want your spawn function to run you need to call it after creating it.

spawn(function()

)()

By adding the () you call your function as you create it. The coroutine alternative to this approach would look something like this

coroutine.wrap(function()

)()

Reminder to take this with a grain of salt, I’m not sure if spawn has he same behavior as coroutine, so my fix might not actually work. However, I do suggest you use coroutines instead of spawn due to optimisation.

1 Like

Thank you, but the rest of the part with spawn( function() works (I’m going to change them to coroutine.wrap later), but only this part doesn’t work. As I said, the “wait works” print doesn’t print out, and I think that’s the reason that the script inside the while wait() doesn’t work.

What type of script is that? A local script, server script? Where is it located.

1 Like

You might have another loop or function preceding this that’s waiting to break/return before that runs. That’s the only way I can see that would break this

1 Like

I’m not sure what is wrong on your end, but the code provided works for me. As a side note, I would recommend the following:

  • Using coroutines: To mimic the behaviour of spawn() you can do
coroutine.wrap(function()
	-- Code here
end)()
  • Using the new task library: The old wait() is throttled meaning that there is no guarantee of when it will run. The new task.wait() has the same functionality but a more accurate length in laggy environments.
  • Improved statements: while true and task.wait() do is the same as doing while task.wait() do. This makes code more readable (and possibly ever so slightly faster but not enough to be noticeable).
2 Likes

It’s a script, and its located in the Workspace.

Yes, there are other loops. I asked about why this script is not working many times, and even if I fixed it, the script keeps on not working.

Do you want to see the full script?

Yes, that would be helpful if you don’t mind

1 Like

It is because you have an infinite while loop inside another infinite while loop. That will cause the first loop to only run once. Because the seconds loop will never stop and therefore, the first loop will not loop.

1 Like

Try this:

-- 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() do
		car:PivotTo(car:GetPivot() * CFrame.new(0, 0, -0.1))
		print("Car moves forward")
	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 ()
	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)
1 Like

Thanks, it seems to work (The output prints out everything except for “No left”, and “No right”), but the problem now is that the car doesn’t turn. I don’t know why.

Does the “works works” statement get printed out?

1 Like

Yes, I said that the output prints out everything except for “No left”, and “No right”.

Here are multiple solutions that I could think of which might solve the problem.

  1. It could be because you are only changing the right and left values once which runs when the player joins, so you should put those if statements inside the loop too?

I pretty sure the solutions below will NOT solve the problem though

  1. It could be that you are only changing the right by itself and the left by itself? Try to make the left false when right is true and the right false when left is true.

  2. Try to change the code like if right and not left to if right == true and left == false and if left and not right to if left == true and right == false (LOL this will definitely not change anything, just giving this out incase it does something yk)

1 Like

It would be appreciated if you would set it as solution so that other people will be able to find the solution faster :slight_smile:

Sorry, but your fixes only printed out in the output, but the car didn’t turn. I want to figure out why the car didn’t turn.

In your old code, did the car turn?