Tweening Error regarding multiple GUIs

Im trying to make a popup animation for a clicking game, however the coins wait after each other to start tweening and some of them get stuck.

Heres the code

Current = 0
local Player = game.Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local FormatNumber = require(ReplicatedStorage.Libs.FormatNumber.Simple)

while wait() do
	if Current ~= Player:WaitForChild("leaderstats").Coins.Value then
		local START_X = math.random(175, 825) / 1000
		local TWEEN_Y = math.random(150, 625) / 1000
		local CoinPopup = game.ReplicatedStorage.CoinPopup:Clone()
		CoinPopup.Position = UDim2.new(START_X, 0, 1, 0)
		CoinPopup.Parent = script.Parent.Popups
		CoinPopup.Amount.Text = "+"..(FormatNumber.FormatCompact(Player.leaderstats.Coins.Value - Current))
		Current = Player:WaitForChild("leaderstats").Coins.Value
		CoinPopup:TweenPosition(UDim2.new(START_X, 0, TWEEN_Y, 0), "Out", "Sine", 0.4)
		wait(.4)
		CoinPopup:TweenSizeAndPosition(UDim2.new(0.021, 0,0.041, 0), UDim2.new(0.042, 0,0.551, 0), "In", "Sine", .2)
	end
end
2 Likes

local Current = 0
local Player = game.Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local FormatNumber = require(ReplicatedStorage.Libs.FormatNumber.Simple)

while wait() do
    coroutine.create(coroutine.resume(function()
        if Current ~= Player:WaitForChild("leaderstats").Coins.Value then
            local START_X = math.random(175, 825) / 1000
            local TWEEN_Y = math.random(150, 625) / 1000
            local CoinPopup = game.ReplicatedStorage.CoinPopup:Clone()
            CoinPopup.Position = UDim2.new(START_X, 0, 1, 0)
            CoinPopup.Parent = script.Parent.Popups
            CoinPopup.Amount.Text = "+" .. (FormatNumber.FormatCompact(Player.leaderstats.Coins.Value - Current))
            Current = Player:WaitForChild("leaderstats").Coins.Value
            CoinPopup:TweenPosition(UDim2.new(START_X, 0, TWEEN_Y, 0), "Out", "Sine", 0.4)
            wait(0.4)
            CoinPopup:TweenSizeAndPosition(UDim2.new(0.021, 0, 0.041, 0), UDim2.new(0.042, 0, 0.551, 0), "In", "Sine", 0.2)
        end
    end))
end

The problem in your code is that „wait(0.4) yields the thread, therefore stopping the while loop from running again. A coroutine is a special function which will run in a separate threat and cannot be stopped by a wait() in the main thread.

I am on my phone right now and therefore might have done a mistake when setting brackets.

1 Like

Ahh yes, creating a coroutine did seem to work however the UI does still get stuck every now and then.

Any errors in output when it freezes? How long does it freeze for?

No errors, it freezes for ever, basically it’s just not playng the second tween sometimes

i think thats because youre using :tweenposition and it breaks if you create more than one tween at a time, my recommendation is using tweenservice instead since it doesnt break when you have more than one tween playing at the same time.