Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a part go from point A to point B in a constant loop with 1 second pauses?

Asked by 8 years ago

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

0
Did you even read both answers... If you do position if a person is standing where the part is, it'll teleport ontop of the person. I wrote that answer in 40 minutes... Gets me so angry. EzraNehemiah_TF2 3552 — 8y
0
I'm not saying to accept mine or anything, but I feel as if you didn't even glance at my answer. EzraNehemiah_TF2 3552 — 8y
0
I did read both answers, I just saw the first one and tried it, and it worked. I apologize for wasting your time :( ReynaldoVictarion 70 — 8y
0
I just wanted to know if you read both. Since I know you read it it's not a waste of time! You're good don't worry! EzraNehemiah_TF2 3552 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago

Please use code blocks to demonstrate segments of your code

Problem

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.

Solution

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.

0
Thanks! It worked just as I wanted it! ReynaldoVictarion 70 — 8y
Ad
Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

What you could do?

Don't use functions, just use a loop! Use CFrame instead of Position, it's better for moving parts.


CFrame

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

How would you loop them together?

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.


Final Product

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!

Answer this question