[TweenService] UI Flicker Issue

function CatchablePet:OnStart()
    local RobloxInstance = self.Instance
    self:CreateUI()
    self.Trove = RunService.RenderStepped:Connect(function()
        local UI = RobloxInstance.PrimaryPart:FindFirstChildOfClass("BillboardGui")
        local Character = Player.Character or Player.Character:Wait()
        local HRP = Character:WaitForChild("HumanoidRootPart")
        local Distance = (RobloxInstance.PrimaryPart.Position - HRP.Position).magnitude
        if self.Range > Distance then
            if UI.Enabled ~= true then
                self:EnableUI(UI)
                UI.Enabled = true
            end
        elseif self.Range <= Distance then
            if UI.Enabled ~= false then self:DisableUI(UI) UI.Enabled = false end
        end
    end)
end

function CatchablePet:CreateUI()
    local RobloxInstance = self.Instance
    local UI = ReplicatedStorage.Misc.CatchableUI
    local UIClone = UI:Clone()
    UIClone.Parent = RobloxInstance.PrimaryPart
end

function CatchablePet:EnableUI(UI)
    local initialSize = UDim2.new(0.1, 0, 0.1, 0)
    local finalSize = UDim2.new(0.5, 0, 0.5, 0)
    UI.Frame.Size = initialSize
    local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
    local tween = TweenService:Create(UI.Frame, tweenInfo, {Size = finalSize})
    tween:Play()
    tween.Completed:Wait()
    tween:Destroy()
end

2 Likes

This is in the wrong category! Use code review only for working code. If you have problems with your code use scripting support instead.

Anyways the problem you’re facing is that the tweens are overlapping. To fix this you could do the following:

function CatchablePet:EnableUI(UI)
    local initialSize = UDim2.new(0.1, 0, 0.1, 0)
    local finalSize = UDim2.new(0.5, 0, 0.5, 0)
    UI.Frame.Size = initialSize
    local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)

	if self.CurrentTween then
		self.CurrentTween:Cancel()
		self.CurrentTween:Destroy()
	end

    self.CurrentTween = TweenService:Create(UI.Frame, tweenInfo, {Size = finalSize})
    self.CurrentTween:Play()
end
1 Like