Changing this while loop into a runservice

Hi,

I want to change this while loop into a runservice connection but I don’t know where to start. Can anyone help me?

		while Label.Parent do
			TweenService:Create(Label, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = UDim2.fromOffset(0, 2)}):Play()
			wait(0.5)
			TweenService:Create(Label, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = UDim2.fromOffset(0, -2)}):Play()
			wait(0.5)
		end

Why are you wanting to change this into a runservice connection?
By the looks of things, using a while loop would be more applicable in this case.
(Unless you are wantinng to ditch the tweens altogether, which I also wouldn’t recommend)

I want to be able to disconnect the loop instead of it having to rely if label.parent exists

Repeat until loop? on a task.defer thread?

It would probably just be easiest for you to create another variable (initially set to true), which you can then AND along with Label.Parent. Then, just change the newly created variable to false to exit the loop.

e.g.

local DoTweenLoop = true
while Label.Parent and DoTweenLoop do
		TweenService:Create(Label, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = UDim2.fromOffset(0, 2)}):Play()
		task.wait(0.5)
		TweenService:Create(Label, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = UDim2.fromOffset(0, -2)}):Play()
		task.wait(0.5)
end

--- other code
DoTweenLoop = false

Hi man, I want to help ya.


—Transition to the helping—


Ok, I want to say that using while loop for tweenservice is not the best way to handle UI animations because it can cause performance issues.. and that’s sucks :frowning:

So, runservice is a better way for this scenerio.

Anywaysss, here is a code by a scripter called Gemini (cuz I don’t know how

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local Label = -- Your text ;)

local TweenUp = TweenService:Create(Label, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = UDim2.fromOffset(0, 2)})
local TweenDown = TweenService:Create(Label, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = UDim2.fromOffset(0, -2)})

local CurrentTween = TweenDown

local Connection = RunService.Heartbeat:Connect(function()
	if not Label.Parent then
		Connection:Disconnect()
		return
	end

	if CurrentTween.PlaybackState == Enum.PlaybackState.Completed then
		if CurrentTween == TweenUp then
			CurrentTween = TweenDown
		else
			CurrentTween = TweenUp
		end
		CurrentTween:Play()
	end
end)

And that’s all.

Btw, the function is called Connection to store the connections to it, and the

 if not Label.Parent then
		Connection:Disconnect()
		return
	end

is to check if the text is still in da game, and if it’s true (the text isn’t in the game), the connection will disconnect and stops the function from running if the label is gone, and it exits the function immediately (return). I hope it will work for ya


TRANSITIONING