Yo, so I am trying to make this smooth animation, and am using the same code to animate both the button and text, I want to make them both smooth, but the text is unbelievably choppy, anyone have any ideas?
Hierarchy:
Video:
Code:
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local button = script.Parent
local label = button:WaitForChild("Text")
local baseSize = button.Size
local baseLabelSize = label.Size
local pulseAmplitude = 0.08
local pulseSpeed = 1.5
local hovering = false
local pulsing = false
local clickedWhileHovering = false
local startTime = 0
local clickScale = 0.92
local tweenInfo = TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local function precise(val, decimals)
local mult = 10 ^ decimals
return math.floor(val * mult + 0.5) / mult
end
local function getScale(t)
local sineValue = math.sin(t * math.pi * pulseSpeed)
local scale = 1 + sineValue * (pulseAmplitude / 2)
return precise(scale, 9)
end
local function applyScale(scale, base)
return UDim2.new(
base.X.Scale * scale,
base.X.Offset,
base.Y.Scale * scale,
base.Y.Offset
)
end
local function tweenTo(scale)
local goal = {
Size = applyScale(scale, baseSize)
}
local labelGoal = {
Size = applyScale(scale, baseLabelSize)
}
TweenService:Create(button, tweenInfo, goal):Play()
TweenService:Create(label, tweenInfo, labelGoal):Play()
end
button.MouseEnter:Connect(function()
hovering = true
if not clickedWhileHovering then
pulsing = true
startTime = tick()
end
end)
button.MouseLeave:Connect(function()
hovering = false
clickedWhileHovering = false
pulsing = false
button.Size = baseSize
label.Size = baseLabelSize
end)
button.MouseButton1Down:Connect(function()
pulsing = false
clickedWhileHovering = true
tweenTo(clickScale)
end)
button.MouseButton1Up:Connect(function()
tweenTo(1)
end)
RunService.RenderStepped:Connect(function()
if pulsing then
local t = tick() - startTime
local scale = getScale(t)
button.Size = applyScale(scale, baseSize)
label.Size = applyScale(scale, baseLabelSize)
end
end)