Playing an Animation from multiple loaded Animations

I’ve been rewriting my rig script for a while now and I encountered a problem that might be simple to fix. What I’m attempting to create is a smaller script that preloads all the animations and can play a specific animation when requested.

I have the loop written down so that it does load the animations to a rig, but I can’t figure out a way to actually play the animations without directly making a variable then playing it… Is there a method, or is this not viable at all and I would have to load each and every animation with a variable to play them?

I also thought about using like nested tables to achieve this, but still. Any help would be greatly appreciated. Willing to provide more script information if needed.

New Animation Loading function.

local function SetupRigAnimations(Player, P1P2)
	-- First grab the Animation set thats EQUIPPED from Player.
	local AnimSet = Main.Inv_CheckPlrItemValue(Player.Name, "DevelopmentSet" .. P1P2, "EQUIPPED")
	local Data = AnimSet:GetChildren()
	local Rig = game.Workspace.PlayerRigs:FindFirstChild(Player.Name, true)
	for i = 1, #Data do
		local Anim = Instance.new("Animation")
		Anim.AnimationId = tostring(Data[i].Value)
		print(Anim.AnimationId)
		Rig.Humanoid.Animator:LoadAnimation(Anim)
		print("Animation loaded successfully:", Data[i], ", for:", P1P2)
	end
end

Old version which played animations:

P1StandingIdle.AnimationId = "rbxassetid://16715749873"
P1WalkForward.AnimationId = "rbxassetid://16804478030"
P1WalkBackward.AnimationId = "rbxassetid://16804479545"
P1Crouching.AnimationId = "rbxassetid://16804481184"
P1CrouchingIdle.AnimationId = "rbxassetid://16804485315"
P1DashtoDive.AnimationId = "rbxassetid://16804484183"
P1DashStart.AnimationId = "rbxassetid://16804602501"
P1Dashing.AnimationId = "rbxassetid://16804604281"
P1DashEnd.AnimationId = "rbxassetid://16804603477"
P1Prone.AnimationId = "rbxassetid://16890382796"


-- Player 2

-- Loads animations
local P1StandingIdleTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1StandingIdle)
local P1WalkForwardTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1WalkForward)
local P1WalkBackwardTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1WalkBackward)
local P1CrouchingTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1Crouching)
local P1CrouchingIdleTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1CrouchingIdle)
local P1DashtoDiveTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1DashtoDive)
local P1DashStartTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1DashStart)
local P1DashingTrack 
local P1DashEndTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1DashEnd)
local P1ProneTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1Prone)


-- This will just run off the side for now but will be loaded in once we get lobbies setup
P1StandingIdleTrack = P1Rig.Humanoid.Animator:LoadAnimation(P1StandingIdle)
P1StandingIdleTrack:Play()
3 Likes

This is just a small script which loads all animations from a folder and uses key-value pairs to save the name and id so that the animation can be played at a later time.

If you want to get the animation using the ID, use LoadedAnimIds[AnimationId].
If you want to get the animation using the Animation Name, use LoadedAnimNames[Name].

local LoadedAnimIds: { [string]: AnimationTrack } = ({});
local LoadedAnimNames: { [string]: AnimationTrack } = ({});

function LoadAnimationsFromList(folder: Folder): ()
    for i: number, v: Instance in ipairs(Folder:GetChildren()) do
        if (v:IsA("Animation")) then
            local AnimationTrack = Animator:LoadAnimation(v);
            LoadedAnimIds[v.AnimationId] = AnimationTrack;
            LoadedAnimNames[v.Name] = AnimationTrack;
        end;
    end;
end;
1 Like

You can modify this script according to your needs, this is just a simple script which can be built upon easily.

It works! Thank you so much for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.