Help making a script repeat itself

Hey everyone! :wave:

I’m currently making a StarterPlayerScript for keyframes and I’m wondering how to make the script loop (repeat) over and over rather than having multiple lines of the same script where it ends eventually. Would anyone know how to do this?

Script:

local TweenService = game:GetService("TweenService")

local camera = game.Workspace.Camera

local cutsceneTime = 5

local StarterGui = game:GetService("StarterGui")

StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

local tweenInfo = TweenInfo.new(

cutsceneTime,

Enum.EasingStyle.Sine,

Enum.EasingDirection.Out,

0,

false,

0

)

function tween(part1,part2)

camera.CameraType = Enum.CameraType.Scriptable

camera.CFrame = part1.CFrame

local tween = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})

tween:Play()

wait(cutsceneTime)

camera.CameraType = Enum.CameraType.Custom

end

wait(1)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

tween(game.Workspace.Test3,game.Workspace.Test4)

tween(game.Workspace.Test5,game.Workspace.Test6)

tween(game.Workspace.Test7,game.Workspace.Test8)

tween(game.Workspace.Test1,game.Workspace.Test2)

Thank you in advance for your assistance! :pray:

You could use a for loop like this

for i = 1, 4, 1 do
    if i == 1 then
        local finished = tween(game.Workspace.Test1,game.Workspace.Test2)
        repeat until finished
    elseif i == 2 then
        local finished = tween(game.Workspace.Test3,game.Workspace.Test4)
        repeat until finished
    elseif i == 3 then
        local finished = tween(game.Workspace.Test5,game.Workspace.Test6)
        repeat until finished
    else
        local finished = tween(game.Workspace.Test7,game.Workspace.Test8)
        repeat until finished
    end
end

The problem with this currently is in the tween function, you would need to add at the end tween.Completed:Connect(function() return true end)
Hope this helps!

For loops,

Here is one way to do it.

First you gotta recognize the pattern

1,2
3,4
5,6
7,8
Using maths, I identified that the pattern is the first column is an arithmetic sequence of odd numbers 1,3,5,7 and the second one is a arithmetic sequence of even numbers 2,4,6,8 that goes iterates in sequence 4 times.

Additionally, you gotta account the Test word behind the number since for loops can only deal with numbers.

You also need to know string indexing using square brackets [] to combined “Test” and the numbers that is being repeated like Test1,Test2 and so forth in order to index workspace to combine the numbers we get from the for loop and the string Test.

game.Workspace.Test1 = game.Workspace["Test1"]

This should be the result for loop one.

for i=1,4 do
	local oddNumberSequence = 1+2*(i-1)
	local evenNumberSequence = 2*i
	tween(workspace["Test"..oddNumberSequence ],workspace["Test"..evenNumberSequence ])
end

Then this repeats again in another for loop so you slap another for loop onto there and iterate it how many times you want

for iterations=1,10 do--repeat 10 times
for i=1,4 do
	local oddNumberSequence = 1+2*(i-1)
	local evenNumberSequence = 2*i
	tween(workspace["Test"..oddNumberSequence ],workspace["Test"..evenNumberSequence ])
end
end
1 Like

That wouldn’t work because the value you returned is already there it will always be false.

Also you shoudn’t use repeat until finished as that would ‘clog up the task scheduler’.
Still don’t use wait() in that case, use RunService.Heartbeat:Wait();

But still:

You should use tween.Completed:Wait() which is the fastest method and less expensive method as well.

1 Like