varTween - For all your variable tweening needs

Get the module Here

The other day I was trying to bulk tween an aspect ratio constraint and noticed there’s really no good way todo it currently. Thus I made varTween.

Currently in order to tween a variable you need to use an external value or make a runservice script and use tweenservice:GetValue(). This module basically works as a glorified wrapper to tween service that keeps track of values for you. I honestly made this for myself but figured alot of other people probably also have the same annoying headache. The module is pretty drop in with only two methods but gets the job done just fine.


Benefits to using varTween:

  • Easy to implement
  • You can access tweens across scripts
  • works with numbers, strings, and tables
  • Probably faster?

Example Usage
local varTween = require(game.ReplicatedStorage.varTween)

local TweenKey = varTween.TweenInit(200, 2, 5, {EasingStyle=Enum.EasingStyle.Exponential, EasingDirection=Enum.EasingDirection.Out, RepeatCount=0, Reverses=false, TweenDelay=0, AutoGarbageCollect=true, AutoGarbageCollectDelay=3})
--makes the tween and returns a tween key
game:GetService("RunService").RenderStepped:Connect(function()
	print(TM.TweenRecall(TweenKey))
--you have to call .TweenRecall(Key) to get the new value and it works independently
--of framerate using time()

--Behavior: Number starts at 2 -> goes to 200 after 5 seconds -> waits 3 more seconds to garbage collect
--all params are optional and so is the starting value
end)
Documentation

Key[any but defaults to int] = module.TweenInit(
Goal [number, string, or table] (required),
Start [same type as goal] | defaults to one of the following: 0, “”, {}
TweenTime [number] (required),

Params (Optional):

{EasingStyle [Enum] | **defaults to quad**,
EasingDirection [Enum] | **Defaults to InOut**, 
RepeatCount [int] | **defaults to 0**, 
Reverses [bool] | **defaults to false**, 
TweenDelay [number] | **defaults to 0**,
AutoGarbageCollect [bool] | **defaults to true**,
AutoGarbageCollectDelay [number] | **defaults to 0.2**

, OverrideKey [any] (Optional) | you can override an existing tween with this key
})

value = module.TweenRecall(Key)

return guide:

Table: returns a table going in order up to value
ex: {“val1”, “val2”, “val3”} and a 66% done tween:

{“val1”, “val2”}
(supports dictionaries)

String: basically works like string.sub but tweens
ex: “hello!” and a 50% done tween:

“hel”

Number: its just a number tween
ex: 10 and a 50% done tween:

5

Nil: means that tween was garbage collected

nil

Is this module useful:

  • Yes
  • No

0 voters

5 Likes

varTween 1.1:

module.GarbageCollect(Key)

manually deleted the key

module.TweenRecallRaw(Key)

recalls the raw data input rather than the output value

Also finally some video and extensive script examples:

Coins going up example

Watch 160eb7554e407022854327aaebd7c114 | Streamable

Code:

local LastCheck = time()
game:GetService("RunService").RenderStepped:Connect(function()
	if time() - LastCheck > 0.1 then
		LastCheck = time()
		local Data = DataRep.RecallData({"Money", "Bricks", "XP"})
		for i, v in pairs(Data) do	
			local TC = VarTween.TweenRecallRaw(i)
			if TC then	
				if TC.goal ~= v then
					local Value = VarTween.TweenRecall(i)
					VarTween.TweenInit(v, Value, 1, {AutoGarbageCollect = false}, i)
				end
				else
				VarTween.TweenInit(v, 0, 1, {AutoGarbageCollect = false}, i)
			end
		end
	end
	
	local CoinsStats = VarTween.TweenRecall("Money")
	local BricksStats = VarTween.TweenRecall("Bricks")
	if CoinsStats then
		script.Parent.Coins.Frame.StatsFrame.TextLabel.Text = math.ceil(CoinsStats)
	end
	if BricksStats then
		script.Parent.Coins.Frame.StatsFrame.BrickLabel.Text = math.ceil(BricksStats)
	end
	
	
	
	
end)

and two other examples i don’t feel like providing code for:


1 Like

Have you used TweenService:GetValue()? sounds easy to add, but yet useful.

this module works as a wrapper for it. It provides extra arguments and coordinates things for you like reverses and delays. Also it supports strings and tables natively so you can tween those. Finally since its a module you can save resources making one tween and calling it between scripts instead of making a bunch of tweens in every script. Once you actually unpack it and use it you will also find alot of more narrow use cases like you can see when a tween ends and coordinate two tweens at once using it. Or do interscript communication with a vartween deletion. Everything uses keys instead of metatables to keep it simple and easily recallable as well.

1 Like