How could i improve this module?

Hello! im trying to improve my scripting abilities and career so i made a pretty basic module to load and play animations the thing is it isnt rlly good and its pretty simple and i would like to know ways that i could improve it

local AnimationHandler = {}


local Data = {
    AnimsFolder = nil,
    AnimsFolder2 = nil,
    Animator1 = nil,
    Animator2 = nil
}

local Tracks = {

}



function AnimationHandler.LoadAnimations(Animator : Animator, Animations, Animator2 : Animator)
    
    
    
    for i, v in pairs(Animations) do
    local clone =    v:Clone()

    
        table.insert(Tracks,clone)
    end

    
    for i,v:Animation in pairs(Animations) do
    
    Animations[i] = Animator:LoadAnimation(v) 
    Data.AnimsFolder = Animations
        
    end
    
    for i, v:Animation in pairs(Tracks) do
        print(v)
        Tracks[i] = Animator2:LoadAnimation(v)

        Data.AnimsFolder2 = Tracks    
    
    end
    

    
end

function AnimationHandler:Play(AnimationName)
    for i, v in pairs(Data.AnimsFolder) do
        
        if v.Name == AnimationName  then
        
                v:Play()

        end
        
        
    end
    
    for i, v in pairs(Data.AnimsFolder2) do
        if v.Name == AnimationName  then

            v:Play()

        end
    end
end


function AnimationHandler:WaitAnimationLength(AnimationName)
    
    for i, v in pairs(Data.AnimsFolder) do

        if v.Name == AnimationName then
            task.wait(v.Length)
        end


    end
end

function AnimationHandler:Stop(AnimationName)
    for i, v in pairs(Data.AnimsFolder) do

        if v.Name == AnimationName then
            v:Stop()
        end


    end
    
    for i, v in pairs(Data.AnimsFolder2) do
        if v.Name == AnimationName  then

            v:Stop()

        end
    end
end


function AnimationHandler:StopAllAnimations()
    for i, v  in pairs(Data.AnimsFolder) do

        if v.IsPlaying then
            v:Stop()
        end


    end
    for i, v in pairs(Data.AnimsFolder2) do

        if v.IsPlaying then
            v:Stop()
        end
    end
    
end


return AnimationHandler

1 Like

I don’t see any reason to actually improve it, it seems fine to me. You can use ipairs instead of pairs sometimes, only when you’re looping through a table like this one — {1, 2, “hi”}, and not this one — {test = “hi”, meee = “NOO!!!”}, and I also recommend renaming the “i” in the for i, v in loops to for _, v in, when you don’t use the “i” variable.

1 Like

Ty! well i would like to improve it since it has some errors n’ stuff but ill do what u recommended to me ^^

1 Like

why so many empty lines!!

also you could make the animations and the tracks table have the keys as the animation names, and in the other functions, you could just access it with

Data.AnimsFolder[AnimationName]
Data.AnimsFolder2[AnimationName]

try to change the :Play function a lil bit:

local Play_Animation = function(Table, name)
    for index, value in pairs(Table) do
        if not value.Name == name then continue end
        value:Play()
    end
end

local Stop_Animation = function(Table, name)
    for index, value in pairs(Table) do
        if not value.Name == name then continue end
        value:Stop()
    end
end

local AnimationHandler = {}


local Data = {
    AnimsFolder = nil,
    AnimsFolder2 = nil,
    Animator1 = nil,
    Animator2 = nil
}

local Tracks = {

}



function AnimationHandler.LoadAnimations(Animator : Animator, Animations, Animator2 : Animator)
    
    
    
    for i, v in pairs(Animations) do
    local clone =    v:Clone()

    
        table.insert(Tracks,clone)
    end

    
    for i,v:Animation in pairs(Animations) do
    
    Animations[i] = Animator:LoadAnimation(v) 
    Data.AnimsFolder = Animations
        
    end
    
    for i, v:Animation in pairs(Tracks) do
        print(v)
        Tracks[i] = Animator2:LoadAnimation(v)

        Data.AnimsFolder2 = Tracks    
    
    end
    

    
end

function AnimationHandler:Play(AnimationName)
    Play_Animation(Data.AnimsFolder, AnimationName)
    Play_Animation(Data.AnimsFolder2, AnimationName)
end


function AnimationHandler:WaitAnimationLength(AnimationName)
    
    for i, v in pairs(Data.AnimsFolder) do

        if v.Name == AnimationName then
            task.wait(v.Length)
        end


    end
end

function AnimationHandler:Stop(AnimationName)
    Stop_Animation(Data.AnimsFolder, AnimationName)
    Stop_Animation(Data.AnimsFolder2, AnimationName)
end


function AnimationHandler:StopAllAnimations()
    for i, v  in pairs(Data.AnimsFolder) do

        if v.IsPlaying then
            v:Stop()
        end


    end

    for i, v in pairs(Data.AnimsFolder2) do

        if v.IsPlaying then
            v:Stop()
        end
    end
    
end


return AnimationHandler

You could try looking into OOP (Object Oriented Programming) and then make this work for anything that you want to animate

sorry for the empty lines! its my way of organizing my code lol ty for the feedback

Yh im tryna learn oop ty though!

ok ill keep that in mind tysm!