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

How do I make a script play multiple times on click?

Okay so I want a script to play 5 times but without copying the same script over and over. How do I this?

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()

2 comments

1

Initializing i at 0 will actually cause the loop to run 6 times.

0

Thanks Ekkoh, I forgot... D:

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