Why isn't my GUI tweening?

I get the prints just fine. But it doesn’t move at all. And no errors.

game:GetService("RunService").RenderStepped:Connect(function()
	for _, card in ipairs(cardHolder:GetChildren()) do
		if card:IsA("ImageButton") then
			card.MouseButton1Click:Connect(function()
				if debounce then return end
				debounce = true
				print("Card clicked:", card.Name)
				local currentPos = card.Position
				local newPos = UDim2.new(currentPos.X.Scale, currentPos.X.Offset, currentPos.Y.Scale, currentPos.Y.Offset - 20)
				local flyUp = TweenService:Create(
					card,
					TweenInfo.new(0.4, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
					{Position = newPos}
				)
				flyUp:Play()
				flyUp.Completed:Wait()
				task.delay(0.3, function()
					debounce = false
				end)
			end)
		end
	end
end)

You’re connecting a new MouseButton1Click event every frame… You should only do this once, typically. Start by removing the MouseButton1Click connection to outside the RenderStepped event connection (you probably don’t need the RenderStepped connection at all). Also, consider using an Activated event connection on the ImageButton instead of MouseButton1Click as its the more general, cross-platform method.

I was using this because cards are continuously added and removed from the cardHolder, and when I only used MouseButton1Click, it didn’t work with new cards added later on. I just wrapped it in ChildAdded, it prints but still the animation doesn’t work

If you’re adding cards during runtime then you should connect events when the cards are added. You can do this either by listening to the ChildAdded event (like you mentioned) or by setting up events on new cards wherever they are created. Either way, it would likely make sense (and help with code readability) to separate your event connections into a function. Here is an example which also fixes your debounce:

--Call this function any time a new card is added, passing the card (an ImageButton) as an argument
function setupCardEvents(card: ImageButton)
	local debounce = false --Local variable so other cards don't access the same variable
	card.MouseButton1Click:Connect(function()
		if debounce then return end
		debounce = true
		print("Card clicked:", card.Name)
		local currentPos = card.Position
		local newPos = UDim2.new(currentPos.X.Scale, currentPos.X.Offset, currentPos.Y.Scale, currentPos.Y.Offset - 20)
		local flyUp = TweenService:Create(
			card,
			TweenInfo.new(0.4, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
			{Position = newPos}
		)
		flyUp:Play()
		flyUp.Completed:Wait()
		task.delay(0.3, function()
			debounce = false
		end)
	end)
end
1 Like

Thanks, but still the same issue, it’s printing but not tweening. I don’t understand why the tween doesn’t work, I have a very similar tween in another part of my code that animates them into the card holder and it works just fine.

Hmm… You don’t happen to have some sort of UILayout in cardHolder do you? If you have something like a UIListLayout, UIGridLayout, etc. this will override the Position of each card, preventing it from (visually) tweening.

I didn’t know that… yes they’re all in a UIListLayout. I’ll take it out and get back to you :v:

You can still use a UIListLayout, you will have to adjust the hierarchy of your cards a bit though. You’ll want to essentially “wrap” the card in an invisible container Frame. For example, if your card hierarchy is currently:

Card (ImageButton)
   CardTitle (TextLabel)

Change it to be something like this:

Card (ImageButton) --This is only an ImageButton so we can listen for the Activated event
   CardContents (ImageLabel) --Use the ImageContent of this for the actual image you want to display
      CardTitle (TextLabel)

The second hierarchy allows the Card (ImageButton) to be automatically positioned by the UIListLayout. When Card (ImageButton) is Activated, you can freely tween CardContents (ImageLabel) without it being overridden by a UILayout. Of course, you’ll have to adjust the code a bit based on this change in hierarchy!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.