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)
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.
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.
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
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).
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.
-- 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)
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.
Here are multiple solutions that I could think of which might solve the problem.
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
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.
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)