Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would i simplify this?

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

✓ Accepted answer

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

5 comments

0

Do i put all 3 of these in one script?

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.

0

Where do i put the while?

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

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.