Coroutine.wrap not working as expected?

Hello, so I have a “button ripple” (material design) effect, although I’ve been having issues with the ripple effect having to finish before my other code runs. So, I’ve resorted to

coroutine.wrap()

Although, this prevents the user from seeing the button ripple at all.

Is there a way to fix this?

Without coroutine.wrap()
https://i.gyazo.com/3b7964e8e3206ca8633dace20a7ecdbd.mp4
(Notice how to ripple has to finish before the text changes.)

With coroutine.wrap()
https://i.gyazo.com/6baec1299d96ca90b4e38827dc5befb4.mp4
(Notice how the ripple doesn’t even have time to show before the text changes.)

I at least want the effect to be seen, but I want the text to change in a timely manner.

Code:

-- Button Ripple Function
function api.buttonRipple(Button, X, Y)
	Button.ClipsDescendants = true

	local Ripple = SystemPackages:WaitForChild('Circle'):Clone()
	Ripple.Parent = Button
	local PosX = X - Ripple.AbsolutePosition.X
	local PosY = Y - Ripple.AbsolutePosition.Y
	Ripple.Position = UDim2.new(0, PosX, 0, PosY)
	
	local Size = 0
	
	if Button.AbsoluteSize.X > Button.AbsoluteSize.Y then
		Size = Button.AbsoluteSize.X * 1.5
	elseif Button.AbsoluteSize.X < Button.AbsoluteSize.Y then
		Size = Button.AbsoluteSize.Y * 1.5
	elseif Button.AbsoluteSize.X == Button.AbsoluteSize.Y then																																																																														print("This place uses a model by Come0n.") --please do not remove!
		Size = Button.AbsoluteSize.X * 1.5
	end
	
	Ripple:TweenSizeAndPosition(UDim2.new(0, Size, 0, Size), UDim2.new(0.5, -Size/2, 0.5, -Size/2), "Out", "Quad", 0.5, false, nil)
	
	for i = 1, 10 do
		Ripple.ImageTransparency = Ripple.ImageTransparency + 0.01
		wait(0.5 / 10)
	end
	
	Ripple:Destroy()
end

-- Local Script
PremiumButton.MouseButton1Click:Connect(function()
	coroutine.wrap(function()
		API.buttonRipple(PremiumButton, Mouse.X, Mouse.Y)
	end)
	
	RoomTypeText.Text = 'Premium'
	RoomTypeScreen.Enabled = false
end)

coroutine.wrap needs to be called, but also you are typing more than you potentially need to. The direct fix is to call it:

	coroutine.wrap(function()
		API.buttonRipple(PremiumButton, Mouse.X, Mouse.Y)
	end)() -- parentheses

But you can clean up your code this way:

coroutine.wrap(API.buttonRipple)(PremiumButton, Mouse.X, Mouse.Y)
1 Like

It still does the same. Would the only way to fix this be lowering the time in my ripple function?

Any help is appreciated. :slight_smile:

Well I would imagine that this is a symptom of disabling the GUI immediately. Try putting this line specifically inside of the coroutine in the first example I gave.

	coroutine.wrap(function()
		API.buttonRipple(PremiumButton, Mouse.X, Mouse.Y)
        RoomTypeScreen.Enabled = false
	end)()