How to play an Animation instead of C-frame

Hello there,

I am trying to figure out how I can change my script from using c-frame to playing an animation. Does anyone know how I would be able to do that?

Code
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local equipped = false
local up = false
local shield = script.Parent.Parent
local handle = script.Parent.Parent:FindFirstChild("Handle")
---------------------CODE BELOW--------------------------
print("Step0")
player:GetMouse().KeyDown:connect(function(k)
    if not character:findFirstChild("Left Arm") then return end
    if not character:findFirstChild("Torso") then return end
    if not character.Torso:findFirstChild("Left Shoulder") then return end
    local lsh = character.Torso:findFirstChild("Left Shoulder")
    if k == "q" then --Spawns the Shield to the Left Arm
        equipped = not equipped
        up = false
        lsh.Part1 = character["Left Arm"]
        print("Step1")
        if equipped then
            for n,i in pairs(shield:GetChildren()) do
                if i.Name ~= "Handle" and i:IsA("BasePart") then
                    local nw = Instance.new("Weld",i)
                    nw.Part0 = handle
                    nw.Part1 = handle
                    nw.C0 = handle.CFrame:inverse()
                    nw.C1 = i.CFrame:inverse()
                end
            end
            handle.Anchored = false
            print("Step2")
			shield.Parent = character
            shield:MakeJoints()
            local nw = Instance.new("Weld",character["Left Arm"])
            nw.Part0 = character["Left Arm"]
            nw.Part1 = handle
            nw.C1 = CFrame.new()
            nw.Name = "LeftGrip"
        else
            if character["Left Arm"]:FindFirstChild("LeftGrip") then
                character["Left Arm"].LeftGrip:remove()
            end
            shield:remove()
            print("Step3")
        end
    elseif k == "e" and equipped then --Raise Shield
        up = not up
        if up then
            lsh.Part1 = nil
            local w = Instance.new("Weld",shield)
            w.Part0 = character.Torso
            w.Part1 = character["Left Arm"]
            w.C1 = CFrame.new(1.2,-.25,0.25) * CFrame.fromEulerAnglesXYZ(math.rad(-75),math.rad(55),0)
        else
            for n,i in pairs(shield:GetChildren()) do
                if i.ClassName == "Weld" then
                    if i.Part0 == character.Torso then
                        i.Parent = nil
                    end
                end
            end
            lsh.Part1 = character["Left Arm"]
        end
    end
end)

Animations are loaded into the humanoid using a localscript. From there you can play or stop them, there really isn’t much to it.

The following example demonstrates the above (assume certain parts are variables even though undefined in the example).

-- Main

Tool.Activated:Connect(function(Mouse) -- Fires when the player clicks w/ the tool.
	
	local AnimTrack = Humanoid:LoadAnimation(AnimationPath) -- i.e script.Parent.Animation, loads animation into humanoid.
	
	AnimTrack:Play() -- Plays animation

end)

Tool.Unequipped:Connect(function(Mouse) -- Fires when player deselects the tool.
	
	Animation:Stop() -- Stops animation
	
end)

Personally, I like to preload my animations so they’re more smooth to begin with, which you can do by calling ContentProvider:PreloadAsync() on the animation.

To integrate it into your tool, you’ll detect when you want the animation to play or stop and follow the same steps from above, however if you’re still unsure I wouldn’t mind giving you a hand with that.

1 Like

What I’m trying to do is change the c-frame in my shield to play as an animation. What my shield does is, if you press the key Q, it spawns on your left arm. Then the key E raises it, basically I am trying to play an animation when you press E instead of a c-frame. Can I still play an animation inside the local script even though it’s not a tool?

Yes, you still can. My above reply was just assuming that you were using a tool, however as long as you’re calling it all form a localscript it should be fine.

Obviously you have to make the animations though, and I have no idea how to go about that, I have an animator in the studio I’m in who just ships each animation to me.

As long as you’re able to create the animations, you’d be able to replace the cframing with them.

1 Like

I already have the animations made so that won’t be a problem.

Do you know how I can preload animations via a local script?

You’d want to use ContentProvider:PreloadAsync({anim1, anim2, anim3}).

You could go about it somewhere along the lines of…

local ContentProvider = game:GetService('ContentProvider')

local Animations = {

    script.Parent.Anim1,
    script.Parent.Anim2,

}

ContentProvider:PreloadAsync(Animations)

Edit: You can find out more about ContentProvider:PreloadAsync() here.

2 Likes

Do you know how I would be able to play the animation on key?

Yeah, so just like in the example I posted above, use UserInputService to detect when a player presses a key and then play the animation.

Example:

-- Variables

local UserInputService = game:GetService('UserInputService')


-- Main

UserInputService.InputBegan:Connect(function(Input, GameProcessed)

    if not GameProcessed then -- Player isn't typing

       if Input.KeyCode == Enum.KeyCode.Q then -- If player presses Q

          Animation:Play()

       end

    end

end
1 Like

When I tried that I got this error:

image
I did

Anim1:Play()

You need to play the AnimationTrack.

local AnimTrack = Humanoid:LoadAnimation(Anim1)
AnimTrack:Play()
2 Likes

Would I need to define the Humanoid?
image

I should of mentioned that above, my mistake.

In a lot of my examples, I just assumed that you’d do so, sorry.

Make sure everything is defined correctly, including your animations and player/humanoid objects.

1 Like

Sorry to bother you again but I got another error
image

Do you know how I can make the Humanoid object a descendant of the game object?

image
This is where the error is

Before you run the animation, make sure Character.Parent isn’t nil.

while Character.Parent == nil do

	Character.AncestryChanged:wait()

end

Which according to Getting error "LoadAnimation requires the Humanoid object to be a descendant of the game object" despite waiting for Character should fix the issue.

This is because the character exists before it is parented to workspace.

1 Like

You can play the animation from a script or a localscript, it doesn’t matter.

And to wait for the character to load, just do something like:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

or:

local Player = game.Players.LocalPlayer
repeat wait() until game.Workspace:FindFirstChild(Player.Name)
local Character = Player.Character
3 Likes

I don’t really see the point of using a script since it replicates, and nice examples. As long as before :LoadAnimation() you’ve made sure the character isn’t parented to nil.

1 Like

You would use a script if it’s running on the server, rather than firing a remote event to the client to play the animation. So basically, you can run the animation client-side or server-side.

2 Likes

In this case calling it from the client is better because he is using UserInputService to detect when a key is pressed. Since it’ll replicate, playing the animation on the server is counter-productive.

It is because of this that I’d always recommend playing animations on the client, that way you don’t need to network from the client to the server which due to latency would feel less responsive for the player.

With that being said, however, if for some reason you’re detecting user input or certain relevant gameplay scenarios from the server to begin with, then playing the animation on the server would most likely be a better solution.

3 Likes