Tween is not playing

Somewhy my tween is not playing if i add task.wait or put it in a loop, but if i dont put task.wait or wrap Tween:Play() in a loop it work nice.

local tweenInfo = TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,1,true)


-- Inside of a function
local goal = {Position = UDim2.fromScale(button.Position.X.Scale,button.Position.Y.Scale * 0.98)}
local Tween = TweenService:Create(button,tweenInfo,goal)
Tween:Play()
--

I tried to find solutions on devforum, but I cant really fix it.

Are you wrapping it in a loop? Why would you do that? Also would be helpful to see the whole code.

Whenever you need to put a Tween into a fast loop, add Tween.Completed:Wait() to make sure the Tween is finished before starting the next one.

i want to make tilt shake ui, heres the code(its module btw)

local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local Tag = "TiltShake"



local tweenInfo = TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,true)


local TiltShakeModule = {}






local function SetUp(button)
	local goal = {Position = UDim2.fromScale(button.Position.X.Scale,button.Position.Y.Scale * 0.98)}
	local Tween = TweenService:Create(button,tweenInfo,goal)
	
	while true do
	if button == nil then break end
	Tween:Play()
	task.wait(math.random(4,7))
	end
end






local function processButton(button)
	-- Check if the object is a GUI button
	if button:IsA("TextButton") or button:IsA("ImageButton") or button:IsA("Frame") then
		
		SetUp(button)
	else
		warn("Tagged object is not a GUI button: " .. button:GetFullName())
	end
end

function  TiltShakeModule.Init()
	-- Process all objects already tagged with Tag
	local buttons = CollectionService:GetTagged(Tag)
	for _, button in ipairs(buttons) do
		processButton(button)
	end

	-- Listen for new objects tagged with Tag after initialization
	CollectionService:GetInstanceAddedSignal(Tag):Connect(function(button)
		processButton(button)
	end)
end

return TiltShakeModule

the wait is longer than time needs tween to finish,so its not the issue.

The tween setup is being done before the while loop, meaning it is constantly trying to run the same tween. You need to move the variables inside the while loop so that it recreates the tween by updating the changes.

But my loop is reversed, so it’s like doesn’t change the anything

I forgot the repeatcount argument in tweeninfo, but the problem is that its not runs even a single time

Just change this:

local function SetUp(button)
	local goal = {Position = UDim2.fromScale(button.Position.X.Scale,button.Position.Y.Scale * 0.98)}
	local Tween = TweenService:Create(button,tweenInfo,goal)
	
	while true do
	if button == nil then break end
	Tween:Play()
	task.wait(math.random(4,7))
	end
end

To this:

local function SetUp(button)
	while true do
		local goal = {Position = UDim2.fromScale(button.Position.X.Scale,button.Position.Y.Scale * 0.98)}
		local Tween = TweenService:Create(button,tweenInfo,goal)
		if button == nil then break end
		Tween:Play()
		task.wait(math.random(4,7))
	end
end

Sorry, but it doesn’t work, I don’t think the problem here.

You put a boolean instead of a number in the repeat count. You need to write there:

local tweenInfo = TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0)

Instead of:

local tweenInfo = TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,true)

EDIT: Oh and:

  • The first 0 is the repeat count. Put -1 if you want it to repeat indefinitely.
  • The true is for repeat.
  • The last zero is for the delay before the tween starts.

yeah, i mentioned in previous reply that i forgot to state repeat count argument, but even when i fix it, it wont work.

Oh, sorry. I didn’t see.

I advise you to try replacing your function with this to troubleshoot (I added prints so you can know what’s wrong):

local function SetUp(button)  
while true do
	local goal = {Position = UDim2.fromScale(button.Position.X.Scale,button.Position.Y.Scale * 0.98)}
	print("a")
	local Tween = TweenService:Create(button,tweenInfo,goal)
	print("b")
	if button == nil then break end
	print("c")
	Tween:Play()
	print("d")
	task.wait(math.random(4,7))
end
end

Open the Output, and see which letters are getting printed.

Is the problem potentially that the size change isn’t visible? It only changes the size by like 2%. You could try setting the 0.98 to something like half (0.5).

The other problem is that the goal of your tween is constant. The goal is only set when the function first runs, so the tween will always go towards the exact same size.

Unless you have other code changing the size, all that will happen is the first tween will play and the other tweens will move the thing towards the same size, which it would already be at.

You might mean to do something like this:

local function SetUp(button)
	while button ~= nil do
		local goal = {Position = UDim2.fromScale(button.Position.X.Scale,button.Position.Y.Scale * 0.98)}
		local Tween = TweenService:Create(button,tweenInfo,goal)
		Tween:Play()
		task.wait(math.random(4,7))
	end
end

This causes the size to decrease by 2% every 4-7 seconds instead of returning to 2% of the original size every 4-7 seconds.