What you want to use is a Numeric For-Loop
According to the Lua 5.0 documentation (available online)
A numeric for has the following syntax:
for var=exp1,exp2,exp3 do
something
end
That loop will execute something for each value of var from exp1 to exp2, using exp3 as the step to increment var. This third expression is optional; when absent, Lua assumes one as the step value.
For example, doing
for i=2,10,1 do
print(i)
end
will print:
2
3
4
5
6
7
8
9
10
Because we are printing i, which starts at 2, and we are also adding 1 to i until we reach 10. Going back to the Lua Documentation:
This third expression is optional; when absent, Lua assumes one as the step value.
This means that we can omit 1 from the for-loop (so it will be for i=2, 10 do)and it will still print the same values.
That's cool and all, but how would I use it?
Given your original script, it looks like you want to smoothly animate the gui from Vector3.new(0,5,0) to Vector3.new(0,8,0) in one second. Four seconds after that, you want to set its Parent to nil. You would execute a for-loop that looks something like this:
gui.Parent = player.Character.Head
for i = 5, 8, 1/30 do
gui.ExtentsOffset = Vector3.new(0,i,0)
wait()
end
gui.ExtentsOffset = Vector3.new(0,8,0)
wait(4)
gui.Parent = nil
Please not that the animation will not take exactly one second, but it should be roughly the same. For animations that take exactly one second, you will have to do delta timing, which is a complex topic outside the scope of this question.
1 comment
Yeah thats what i want to know about, how loops work.