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

How to make a repeat go back to beginning after the "until"?

Asked by 8 years ago

I have this right now,

-- send help

repeat
wait(.5)
script.Parent.Position = script.Parent.Position - 0, 0, 5   
until
script.Parent.Position = -2402.83, 510.332, -895.993
then script.Parent.Position = -2402.83, 510.332, -1320.103
-- how do I make it go back to the repeat process again?

2 answers

Log in to vote
2
Answered by 8 years ago

There are a few of different problems with your script and I will help you fix them

1) Syntax

Your first problem would be the syntax. Position is a Vector3 value, so if you want to do math on it, you have to do it with either a constant or another Vector3 value. The Vector3 constructor is Vector3.new, so you would put that around all the number values. Also, you used the keyword then wrong at the end of your code. then is only supposed to be used in an if statement, like so:

repeat
wait(.5)
script.Parent.Position = script.Parent.Position - Vector3.new(0, 0, 5)
until
script.Parent.Position = Vector3.new(-2402.83, 510.332, -895.993)
script.Parent.Position = Vector3.new(-2402.83, 510.332, -1320.103)

2) Loop

I would highly recommend that you do not use a repeat loop. I don't find them to be very useful. In your situation, you're trying to subtract a constant value from a position until that position is equal to another vector. Well, with the way floating point math works in roblox is that decimals will never be equal to each other because roblox will add other numbers to the decimals. For example, 0.3 will not be equal to 0.3 because roblox will add other digits to the decimal, making it something like 0.29999999998 or 0.30000000001.

In your situation, I would use a for loop, because a for loop iterates only a specific number of times and then stops. Based on your code, you'll want to subtract 0,0,5 from the position 424 times. So the fixed code would be:

for i = 1, 424 do
    wait(0.5)
    script.Parent.Position = script.Parent.Position + Vector3.new(0, 0, 5)
end
script.Parent.Position = Vector3.new(-2402.83, 510.332, -1320.103)

3) Repetition

You want to make the whole process repeat over and over again, so the simplest solution would be a while loop. Basically, a while loop will repeat over and over again until the conditional between the while and the do is false. So in a while true do loop, it will run forever because the true conditional will never become false, like so:

--Final code
while true do
    for i = 1, 424 do
        wait(0.5)
        script.Parent.Position = script.Parent.Position + Vector3.new(0, 0, 5)
    end
    script.Parent.Position = Vector3.new(-2402.83, 510.332, -1320.103)
    wait() --This wait is here to prevent the script from crashing
end

Hope this helped!

Ad
Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

Before i address you original question, lets take a look at the rest of your code.

On line 5, you try to subtract from the Position, but Position is not a number value, nor is it a combination of three numbers. It's a Vector3, so we must edit it using a Vector3. We do this with the Vector3.new constructor.

script.Parent.Position = script.Parent.Position - Vector3.new(0, 0, 5)

You have the same problem on line 7 and 8.


Line 7 is a condition, so we need to use two equal signs (==). A single equal sign means a command. "Computer, make this equal that, now!" But two equal signs signifies a comparison between two values, so we will use that. I like to think of == as meaning does equal, as in, "this does equal that."

script.Parent.Position == Vector3.new(-2402.83, 510.332, -895.993)

Now we come to the then on line 8. This will just cause an error, because then can only (as far as I know) be used with an if statement. It's never a good idea to use something you don't understand, so look it up on the Roblox wiki first. In this case, we can just remove the then.

until
    script.Parent.Position = Vector3.new(-2402.83, 510.332, -895.993)

script.Parent.Position = Vector3.new(-2402.83, 510.332, -1320.103)

As for making this loop over and over again, we need to use a while loop. This loop takes the form of while condition do, repeating until condition equals false.

But we want this to run forever, so we need to pick a condition that will never, ever be false. This is the most easily achieved with the word true, since true will obviously never equal false.

while true do
    --code
end

Answer this question