Basically just the same as @berezaa 's tween wrapper, I was just bored and wanted to make something I guess.
What the function returns:
If it fails to create the tween for the given object with the given properties it will return the error message instead of erroring out completely.
If it successfully creates the tween then it will either return two values, if AutoRun is set to true, it will return true when the tween finishes and call the callBack function if given. If AutoRun is set to false, it will return the Tween instantly and you can use :Play() to play it.
Auto TweenInfo Validation:
You do not need to pass all the tweenInfo arguments, it will automatically put the default values in if any arguments are missing.
If you put invalid arguments, it will replace it with the default valid arguments.
Source:
--> Custom Types
export type tweenInfo = {
Time: number,
Style: Enum.EasingStyle,
Direction: Enum.EasingDirection,
Repeat: number,
Reverses: boolean,
Delay: number,
}
--> Default Valid Tables
local DefaultTweenInfo: tweenInfo = {
Time = 1,
Style = Enum.EasingStyle.Linear,
Direction = Enum.EasingDirection.In,
Repeat = 0,
Reverses = false,
Delay = 0,
}
table.freeze(DefaultTweenInfo)
--> Validations
local function TableToTweenInfo(ValidTable: tweenInfo)
return TweenInfo.new(ValidTable.Time, ValidTable.Style, ValidTable.Direction, ValidTable.Repeat, ValidTable.Reverses, ValidTable.Delay)
end
local function ValidateTweenInfo(Info: tweenInfo)
local ValidTweenInfo = {}
for i, v in pairs(Info) do
if typeof(v) == typeof(DefaultTweenInfo[i]) then
ValidTweenInfo[i] = v
elseif typeof(v) ~= typeof(DefaultTweenInfo[i]) then
ValidTweenInfo[i] = DefaultTweenInfo[i]
end
end
for i, v in pairs(DefaultTweenInfo) do
if ValidTweenInfo[i] == nil then
ValidTweenInfo[i] = v
end
end
return TableToTweenInfo(ValidTweenInfo)
end
--> Create Tween
local function CreateTween(Object: Instance, tweenInfo: tweenInfo, PropertyGoals, AutoRun: boolean?, callBack: () -> ()): Tween? | boolean?
local ValidTweenInfo = ValidateTweenInfo(tweenInfo)
local succ, tween: Tween? = pcall(function()
return game:GetService("TweenService"):Create(Object, ValidTweenInfo, PropertyGoals)
end)
if (succ == true and tween ~= nil) and ((AutoRun ~= nil) and (AutoRun == true)) then
tween:Play()
tween.Completed:Wait()
if callBack ~= nil and typeof(callBack) == "function" then
callBack()
end
return true
end
return tween
end
return CreateTween
Example:
--> Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--> TweenService
local TweenService = require(ReplicatedStorage:WaitForChild("TweenService"))
--> Part
local Part = workspace:WaitForChild("Part", 1)
--> Tween Info
local tweenInfo = {
Time = 10,
Style = Enum.EasingStyle.Linear,
Direction = Enum.EasingDirection.In,
}
local Goal = {
["Size"] = Vector3.new(200, 200, 200),
["Position"] = Vector3.new(0, 10, 0),
}
local Goal2 = {
["Transparency"] = 0.5,
}
--> Tween
local Tween: Tween | string = TweenService(Part, tweenInfo, Goal, false)
local Tween2: Tween | string = TweenService(Part, tweenInfo, Goal2, true, function()
print("Tween2 Finished")
end)
if typeof(Tween) == "string" then --> Creating Tween Failed
warn(Tween)
elseif typeof(Tween) == "Instance" then --> Tween Was Successfully Made
Tween:Play()
end
Pretty useless resource but I was bored so