Skip to content

Instantly share code, notes, and snippets.

@mvyasu
Last active July 11, 2023 02:15
Show Gist options
  • Select an option

  • Save mvyasu/aabfed6050a483f05f5aea7bc133a347 to your computer and use it in GitHub Desktop.

Select an option

Save mvyasu/aabfed6050a483f05f5aea7bc133a347 to your computer and use it in GitHub Desktop.
Runs a tween inside a loop that can be used alongside the task library
local function rawTween(tweenInfo: TweenInfo, tweenCallback: (tweenAlpha: number) -> nil)
local tweenEasingStyle = tweenInfo.EasingStyle
local tweenEasingDirection = tweenInfo.EasingDirection
local tweenRepeatCount = tweenInfo.RepeatCount
local tweenIndefinitely = tweenRepeatCount<=-1
local tweenDelay = tweenInfo.DelayTime
local tweenDuration = tweenDelay + tweenInfo.Time
local tweenReverses = tweenInfo.Reverses
if tweenReverses then
tweenDuration += tweenInfo.Time
end
local TweenService = game:GetService("TweenService")
local tweensRemaining = tweenRepeatCount + 1
while tweenIndefinitely or tweensRemaining>0 do
local startTime = tick() + tweenDelay
local endTime = (startTime - tweenDelay) + tweenDuration
while tick()<endTime do
local timeElapsed = tick()-startTime
if timeElapsed>0 then
local currentAlpha = timeElapsed / (tweenDuration-tweenDelay)
if tweenReverses then
currentAlpha = if currentAlpha>0.5 then -2 * currentAlpha + 2 else 2 * currentAlpha
end
local tweenAlpha = TweenService:GetValue(currentAlpha, tweenEasingStyle, tweenEasingDirection)
tweenCallback(tweenAlpha)
else
tweenCallback(0)
end
task.wait()
end
tweensRemaining = math.max(tweensRemaining-1, 0)
end
--ensures the last tweenAlpha is an integer and not a float
tweenCallback(if tweenReverses then 0 else 1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment