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

Is there a better way to Continuously Tween a Frame without using a loop?

Well we all know that too much while wait() do or while true do can cause crashes, which we don't want those.

Now I learned that if I want to change a text to a certain value then a better way then using loops is doing the .Changed event. But what if I'm trying to continuously Tween a frame? Is there a better way than looping?

This is my script;

local NB = script.Parent
	
while true do
	wait(0.00000000000000000000000000000000001)
	NB:TweenPosition(UDim2.new(0, 840,0, 220),'In','Sine',0.5)
	wait(0.00000000000000000000000000000000001)
	NB:TweenPosition(UDim2.new(0, 840,0, 240),'Out','Sine',0.5)
end

If there is a better way then looping for forever tweens then please help. I need to learn a better way then just looping. It causes crashes and it's annoying.

3 comments

0

While wait() do shouldn't be causing you crashes. While true do should cause crashes if you don't have a yield inside your looped code. Personally I would use a repeat (code here) until loop instead though as you can stop it with a certain condition. Be sure to add a yield though.

0

not necessary to do wait(0.00000...ect1) just use wait()

0

I feel like wait() takes longer

1 answer

✓ Accepted answer

You can:

  1. add a call to wait() either in the code of the loop, or as the condition for the loop.

Example:

local NB = script.Parent
while wait() do
    wait(0.00000000000000000000000000000000001)
    NB:TweenPosition(UDim2.new(0, 840,0, 220),'In','Sine',0.5)
    wait(0.00000000000000000000000000000000001)
    NB:TweenPosition(UDim2.new(0, 840,0, 240),'Out','Sine',0.5)
end
  1. In addition to 1, you can also create an entirely separate thread (script) to handle the loop.

Example:

local NB = script.Parent
spawn(function()
    while wait() do
        wait(0.00000000000000000000000000000000001)
        NB:TweenPosition(UDim2.new(0, 840,0, 220),'In','Sine',0.5)
        wait(0.00000000000000000000000000000000001)
        NB:TweenPosition(UDim2.new(0, 840,0, 240),'Out','Sine',0.5)
    end
end)

Roblox's "spawn" function will run the function in an entirely new thread (a thread is basically another name for a ROBLOX script).

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