I’m trying to make an animation player using modules, upon requiring the main module, it first clones the animation player (which is also a module) then returns it, it works, but, I was wondering if it could be improved with OOP or not.
--
local Handler = {}
function Handler:Start()
local AnimationHandler = script.Main:Clone()
AnimationHandler.Parent = game:GetService("ServerStorage")
return AnimationHandler
end
return Handler
--
local Animation = require(require(game:GetService("ServerScriptService").AnimationHandler).Start()).Init(...)
What you have got is already object-oriented. Object-oriented just means having dedicated objects (tables) for certain tasks. Requiring a module script for the sole purpose of handling animations is object-oriented programming.
Yes, it can, you should have an Animation class that just holds an ID/Animation instance, it should have functions like :Play(), :Pause(), :Destroy(), etc. Also another class, AnimationPlayer, that will have functions like :PlayAnimation(animationObject), :PlayAnimations({ animationObject }), same idea for pause and stop too.
I was just going to link the ModuleScript doc page, but I noticed they really only describe how they work on a basic level. They don’t really explain any best practices, or things to avoid. But this method would be one of the “things to avoid”.
If you’re ever in a situation where you need to clone modules around in order to do what you need to do, there’s a good chance your’re doing that thing wrong (or at the very least, making it more convoluted than it needs to be). Try finding a way where the module can just chill in one spot and be used without having to be moved around or cloned. This is the best practice.
I know that, but the thing is, It’s a module for characters only and I don’t really wanna requiring it for each time character spawns, whilst having to use events to do functions on the character script.
If you don’t want to require a modulescript every single time you want to do something, but want to have a global function, use _G. It’s basically a global variable that you can do anything with. Just be careful, because it’s a global function that any scripts can use. Unlike a modulescript, you can’t filter what you want calling it. (if that makes sense, it’s hard to explain.)