I was thinking about getting rid of this tweenhandler I made in a module script because I thought it was completely useless because I forgot you can literally basically achieve the exact same thing pretty easily. Look.
local TweenHandler = {}
-- Use this for tweens just to make your life easier
local TweenService = game:GetService("TweenService")
function TweenHandler:CreateTween(instance, info, goals)
local Tween = TweenService:Create(instance, info, goals)
return Tween
-- Returned so that you can choose what to do with it (e.g. stop, pause, etc)
end
return TweenHandler
I was also thinking kinda useless because literally an extra script for nothing when I could just do it without these custom things but meh let me know.
You could do something such as creating the tween & playing it. It isn’t major, but you could do something along the lines as:
-- Just a basic example implementation
function TweenHandler:CreateTween(instance, info, goals)
local Tween = TweenService:Create(instance, info, goals)
return Tween
-- Returned so that you can choose what to do with it (e.g. stop, pause, etc)
end
function TweenHandler:PlayTween(Tween)
if Tween and Tween.PlaybackState ~= Enum.PlaybackState.Playing then
Tween:Play()
end
end
function TweenHandler:PauseTween(Tween)
if Tween and Tween.PlaybackState == Enum.PlaybackState.Playing then
Tween:Pause()
end
end
function TweenHandler:StopTween(Tween)
if Tween then
Tween:Cancel()
end
end
function TweenHandler:ResumeTween(Tween)
if Tween and Tween.PlaybackState == Enum.PlaybackState.Paused then
Tween:Play()
end
end
function TweenHandler:GetTweenState(Tween)
if Tween then
return Tween.PlaybackState
else
return nil
end
end
Yes but all these functions feel kinda useless because I’m writing more code when I could just write Tween:Pause() without writing a whole function. Is there any other use other than making it feel fancy?
Yes its essentially useless… both code does the same thing:
function TweenHandler:CreateTween(instance, info, goals)
local Tween = TweenService:Create(instance, info, goals)
return Tween
end
local Tween = TweenService:Create(instance, info, goals)
The difference with the helper functions, is they check if it is in that state already.
So if a tween is already playing, no need to play, if it’s already paused, no need to pause.
Although you could just do:
-- Tween already playing, wait for it to complete
if (createdTween.PlaybackState == Enum.PlaybackState.Playing) then
createdTween.Completed:Wait();
end;
you could cache your tweens and make a handler out of that instead of creating a new one everytime you need to use it.
i swear 90% of roblox games do not cache tweens and instead create them over n over which i feel like is a principle that most ppl avoid in every other aspect of gamedeving.