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.
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 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.
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
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.