Animation and name finding

I’m not sure if this is even done by many people, although is there a certain way to find a section of a model’s name, so if a model was called “RightUpperLeg” for a R15 character and I wanted to find an animation that is inside of a folder that is called “connectRightLeg”, as you see they have similar name because “RightLeg” is in “RightUpperLeg”, so how would I do this so I can play the animation called “connectRightLeg”

for _, Object in pairs(Character:GetDescendants()) do
    local Match = string.match(Object.Name, "RightLeg")
    if Match then
        if Object:IsA("Animation") then
            -- Play Animation
            break
        end
    end
end

This code will check if anything in the Character has “RightLeg” in it’s name, if it does and it’s an animation then it’ll run the code which you’ll replace the comment with and then end the for loop.

That seems right but there are multiple animations that I need, so like “connectRightLeg”, there is also “connectLeftLeg” and “connectLeftHand” too

Well you would remove the break and you would need to edit the code to account for the LeftHand, LeftLeg, animations etc.

But then that would increase the length of the script and make it too long, so that’s why I wanted a shorter method, instead of making it long and manually adding in all the model names and animation names.

If you know the name of all the animations then you can create a dictionary with them in and check if Object.Name is in it.

I would separate each word in the model’s name using string patterns and then loop through all animations to see if they contain those keywords.

function SearchForAnimation(modelName)
    local words = {}
    local currentWeight = 0
    local currentMatch

    for word in string.gmatch(modelName, "%u%l+") do
        table.insert(words, word)
    end

    for _, animation in pairs(folder:GetChildren()) do
        local found = 0
        for i = 1, #words do
            if string.find(animation.Name, words[i]) then
                found = found + 1
            end
        end
        if found > currentWeight then
            currentWeight = found
            currentMatch = animation
        end
    end

    return currentMatch
end

I’m not sure if this works but you get the idea.

2 Likes

I’ll try it out, thanks for this though.