Changing Idle Animation

Hello, i’m experiencing an error with changing the idle animation on a character, to activate a crouching state.

This is my code:

local PathfindingService = game:GetService("PathfindingService")
local RequireService = require(game.ReplicatedStorage.Lib.RequireModule);

repeat
    wait(1)
until script.Parent:FindFirstChild("Head")

local Player = RequireService("Players").LocalPlayer;
local UIS = RequireService("UserInputService");
local Animate
local Humanoid = Player.Character:FindFirstChild('Humanoid');

local States = {
    ["Crouch"] = false;
    ["Run"] = false;
}

local Anims = {
    ["CrouchIdle"] = Humanoid:LoadAnimation(script.CrouchIdle);
    ["CrouchWalk"] = Humanoid:LoadAnimation(script.CrouchWalk);
}

local IsR6 = Humanoid.RigType == Enum.HumanoidRigType.R6;

local Char = Player.Character;


for i,v in pairs(Char.Animate.idle:GetChildren()) do
    print(v.Name)
end

-- PC
UIS.InputBegan:Connect(function(Input, Typing)
    local IKC = Input.KeyCode;
    local KC = Enum.KeyCode;

	if IKC == KC.LeftControl then

		if not IsR6 then
            Char.Animate.idle["Animation1"] = Anims.CrouchIdle.Animation.AnimationId;
            Char.Animate.idle["Animation2"] = Anims.CrouchIdle.Animation.AnimationId;
        end

        Humanoid:ChangeState(Enum.HumanoidStateType.Landed);

        States.Crouch = true;
    end
end)

UIS.InputEnded:Connect(function(Input, Typing)
    local IKC = Input.KeyCode;
    local KC = Enum.KeyCode;

    if IKC == KC.LeftControl then

        if not IsR6 then
            Char.Animate.idle["Animation1"] = "rbxassetid://8420903601"
            Char.Animate.idle["Animation2"] = "rbxassetid://8420900272"
        end

        Humanoid:ChangeState(Enum.HumanoidStateType.Landed);
        States.Crouch = false;
    end
end)

-- Mobile;

-- Mobile is hard ;-;

Humanoid.StateChanged:Connect(function(Old, New)
    if States.Crouch == true then
        if New == Enum.HumanoidStateType.Running then
            Anims.CrouchIdle:Stop()
            Anims.CrouchWalk:Play()
        end
    end
end)

When activating idle, i get this error:

 "Animation1 is not a valid member of StringValue "Workspace.Ix1x0x2.Animate.idle"

Any fixes?

1 Like

Where is the “Animation1” object stored? It seems like your script is trying to get an animation as a child of a stringobject.

3 Likes

The script thinks Animation1 and Animation2 are properties, because you’re not using FindFirstChild and you’re trying to set them directly. The reason the error occurs is because you forgot the .AnimationId property:

Char.Animate.idle.Animation1.AnimationId = "rbxassetid://your_animation_id"
2 Likes

Oh my god, i am so stupid! i didnt even realize that. Thank you so much!