Should I do this a different way?

I’ve been scripting a block to move up and down in a Sine motion. And I wanted to make the tweens infinitely repeat. I was about to write a topic asking how I should do this until I came up with the script below:

local Tween = game:GetService("TweenService")

local Marshmellow = script.Parent

local Up = Tween:Create(Marshmellow, TweenInfo.new(3, Enum.EasingStyle.Sine), {Position = Vector3.new(-31.544, 2.333, 33.1)})

local Down = Tween:Create(Marshmellow, TweenInfo.new(3, Enum.EasingStyle.Sine), {Position = Vector3.new(-31.637, 1.267, 33.1)})

local RepeatStop = false

repeat
    wait(2.5)
    Up:Play()
    wait(2.5)
    Down:Play()
until RepeatStop == true

As you can see, I made a variable called “RepeatStop” and I set it to false. Then, I told the repeat script to repeat the animations until RepeatStop is true. But since there will be no script that will make it true, it will stay false forever, making the animations repeat infinitely.

And I was wondering, is there a different way I should do this? Like, should I be making a repeat script differently, or is this an ok way to do this for future scripts like this?

Thank you!

Hello! Try using a while true do loop instead of a repeat. For example:

local Tween = game:GetService("TweenService")

local Marshmellow = script.Parent

local Up = Tween:Create(Marshmellow, TweenInfo.new(3, Enum.EasingStyle.Sine), {Position = Vector3.new(-31.544, 2.333, 33.1)})

local Down = Tween:Create(Marshmellow, TweenInfo.new(3, Enum.EasingStyle.Sine), {Position = Vector3.new(-31.637, 1.267, 33.1)})

while true do
    wait(2.5)
    Up:Play()
    wait(2.5)
    Down:Play()
end

Good luck and happy holidays! :smiley:

Also: If you need any more help watch this video by AlvinBlox. While Loops - Roblox Beginner Scripting #15 - YouTube

1 Like

That’s not really needed, but you could try using while loops. Like so:

while true do -- While loops keep running while one condition is met. Since this true boolean is anonymous and can't be overwritten anywhere, it'll run infinitely.
    wait(2.5)
    Up:Play()
    wait(2.5)
    Down:Play()
end

If you wish to run code after the loop, you’ll need to create a new thread and put the loop there. You can achieve it by using coroutines.

1 Like