Okay so I want a script to play 5 times but without copying the same script over and over. How do I this?
COMMUNITY QUESTION
How do I make a script play multiple times on click?
2 answers
Using a for loop you can run the same peice of code multiple times, while still only writing it once.
function DoSomething(Num)
print(Num)
end
for i = 1, 5 do -- Start at i = 1 and increase i (by one) until it's more than 5
DoSomething(i)
end
--- Output:
-- 1
-- 2
-- 3
-- 4
-- 5
Just put what you want to execute in a function. Then call it 5 times either using a for loop or calling the function 5 times.
function doCode()
--Do Stuff
end
for i = 1, 5 do
doCode()
end
-- OR
doCode()
doCode()
doCode()
doCode()
doCode()
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Initializing i at 0 will actually cause the loop to run 6 times.
Thanks Ekkoh, I forgot... D: