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

How would i simplify this?

Asked by 9 years ago

I don't want to type this 1000 times because what im trying to do is change 500 and make it one less each time, 500, 499, 498,... and so on all the way to - 500. Is there a much easier way to do this than type workspace.fly.meow.positon.......1000 times?

while true do
workspace.fly1.MEOW.Position = Vector3.new (500, 20, 40)
wait(1)

-- The bottom part is what i want it to end with
----------------------------------------------------------------------------------

workspace.fly1.MEOW.Position=Vector3.new(-500, 20, 40)
wait(1)
end

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

What you're looking for is a for loop. The correct format of for loops looks like this:

for variable = startNumber, endNumber, increment do

end

What this does is variable will start at startNumber, end at endNumber, with increment being added to variable each time the loop iterates.

The default value of increment is 1, so if you're just adding 1 each time you can leave it out.

for index = 1, 10, 0.5 do
    print(index)
end

Play around with these numbers and check your output.


To change the position we just need to make startNumber 500, endNumber -500, and increment -1 so that one is subtracted each time.

for i = 500, -500, -1 do
    workspace.fly1.MEOW.Position = Vector3.new(i, 20, 40)
    wait(1)
end
0
Do i put all 3 of these in one script? QuantumScripter 48 — 9y
2
The first two are just examples of how to use a for loop. The last is the one you need. You could also you a while loop if you want the transition to be smooth and not stud by stud. GoldenPhysics 474 — 9y
0
Where do i put the while? QuantumScripter 48 — 9y
1
I should have included this in my original post, but here's the wiki article you should read: http://wiki.roblox.com/index.php?title=For_loop#For Perci1 4988 — 9y
0
Thanks! QuantumScripter 48 — 9y
Ad

Answer this question