I want to make a Part (Cube), go from point A to point B in a constant loop with 1 second pauses. I already wrote the code. Problem is... when I run the game, it only goes to point B, and stays there. Please help.
Cube = script.Parent
function toPointB() Cube.Position=Vector3.new(22, 0.1, 66) end
function toPointA() Cube.Position=Vector3.new(22, 0.1, 28) end
while true do toPointB() wait(1) toPointA() end
Please use code blocks to demonstrate segments of your code
Every time you call toPointA
, your while loop realizes there's no more code left to execute in it's body, so it then returns back to re-evaluate it's condition (your immutable true
value). This is done pretty much instantaneously. Here's another example:
while true do print("Hello") -- Prints 'Hello' wait(1) -- Wait approximately 1 second print("There") -- Print there -- Immediatelty go back to the beginning without waiting. end
The code above would just continue to print Hello
and There
at the same time with a second interval in between instructions.
Luckily, the solution is really simple! You need to give your loop time to wait before returning back to the beginning of it's body, once it's reached it's end. Since you want these instructions to run every one second, you'd simply add another wait command:
while true do print("Hello") -- Print 'Hello' wait(1) -- Wait print("There") -- Print 'There' wait(1) -- Wait before returning back to the beginning of the loop. end
And that's all. Just apply this to your situation and you should notice the change in position your toPointA
function applies to the cube. Just let me know if you have any questions.
Don't use functions, just use a loop! Use CFrame instead of Position, it's better for moving parts.
CFrame
or Coordinate Frame is a data type that represents a position and orentation in 3D space, meaning that you can set the part's Position and Rotation at the same time.
Part.CFrame = CFrame.new(12,3,6.32) --Position Changed Part.CFrame = Part.CFrame * CFrame.Angles(0,0,math.rad(90)) --Rotation changed; math.rad(90) is equal to math.pi/2 Part.CFrame = CFrame.new(12,3,6.32) * CFrame.Angles(0,0,math.pi/2) --Both position and rotation changed
Use a while
loop like you've already done but instead of while true
you can replace true
with anything as long as it equals true, it'll run. For example:
while a == 1 do --All the following loops WILL run wait() --Wait to prevent lag end while 1 do wait() end while Part.BrickColor == BrickColor.new("White") do wait() end while not false do --not false turns into true therefore it works. wait() end while wait() do --wait() also returns true after the amount of time you put inside the parentheses end
So knowing that while wait() do
works, we can implement that into our code to make an infinite looping code that run every 1 second: while wait(1) do
.
I removed the functions and moved those inside the loop.
Cube = script.Parent while wait(1) do --while wait(1) do shortens your script and saves time, but however way you prefer to write it works. Cube.CFrame=CFrame.new(22, 0.1, 66) --Point B wait(1) Cube.CFrame=CFrame.new(22, 0.1, 28) --Point A end
Hope it helps!