TweenService on Gui Objects

Hello DevForum, I know that there are tween functions for Gui Objects but I would like to use the TweenService as I have already produced code for what I am trying to do. I tried searching for this solution but I could only find a solution for positioning. The reason I am asking for both is so that both answers can be dumped into a singular thread. The question is how would I use TweenService to do the following:

  1. Move the part in a certain direction.
  2. Resize the part.

This is the code I currently have:

coroutine.wrap(function()
	local Tween = TweenService:Create(script.Parent, Info, {Size = UDim2.new(0,145,1,0)})
	Tween:Play()
end)
coroutine.wrap(function()
	local Tween = TweenService:Create(script.Parent.Parent.ImageButton, Info, {Position = UDim2.new(0,145,0.5,0)})
	Tween:Play()
end)

Hello!

The code for the GUI object you have provided looks fine, but will not run as you are not running the coroutines. To do so, one way is:

local coroutineToRun = coroutine.wrap(function()
    -- your code here
end)

coroutineToRun()

or just:

coroutine.wrap(function()
    -- code here
end)() -- with the () at the end as if you're running a function

if you want to be simpler.

However, you do not need coroutines for this matter as tweens do not yield the code when they are played. You should remove them, rename your tween variables and play them, or just straight up do TweenService:Create(your arguments here):Play().

For this, you utilize TweenService the same way as you do with GUI objects, except change the goals “Size” and “Position” to their desired type of value (CFrame (or Vector3 if you need that instead) for position & Vector3 for size)

Hope this helps!

1 Like

Playing a tween does not yield the thread meaning you can remove the coroutine.wraps

1 Like