Animation Script Is Not Working!

Good afternoon!

I was trying to create a mechanic where you touch a chest and it plays an animation for opening said chest. However, this was easier set then done as I have gotten countless errors. The script should be simple.

local Animation = game.StarterPlayer.Animation

if game.workspace.Part.Touched then

Animation:Play()

end

Here is where the animation currently is located.

Capture
The error says that Animation (My variable) Is not a valid member of animation. Moved it multiple times. still says the same thing. Please help!

1 Like

I think you cant go here, put the animation in Replicated Storage

local Animation = game.ReplicatedStorage.Animation
game.workspace.Part.Touched:Connect(function(hit)
   local char = hit.Parent
   local animation = char.Humanoid:LoadAnimation(Animation)
   animation:Play()
end

Alright, I put it there. Right now it still doesn’t play the animation. Thanks for your support though! :smiley: :+1:

1 Like

Couple things:

  • Why are you referencing the Animation Object inside the StarterPlayer? Wouldn’t it be better to put it inside the Chest?

  • Touched is an event, you have to connect it with a function in order to properly detect whenever it gets touched

What I’d do, is put the Animation inside the Chest instead and use a local function whenever the Part gets touched

This should do it:

local Chest = script.Parent
local Animation = Chest:WaitForChild("Animation")

local DB = false

local function Touched(Hit)
    local PlayerCheck = game.Players:GetPlayerFromCharacter(Hit.Parent)

    if PlayerCheck and not DB then
        DB = true

        local Character = PlayerCheck.Character
        local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")

        local LoadAnimation = Animator:LoadAnimation(Animation)
        LoadAnimation:Play()

        LoadAnimation.Stopped:Connect(function()
            DB = false
        end)

    end
end

Chest.Touched:Connect(Touched)

I made other script, look here

Wow, okay, that’s a lot. Let me make sure that works.

I can’t tell if it doesn’t or does work because R6 isn’t working at the moment.

What do you mean by that? Are you sure that the Script is properly enabled? It should account for both R6 & R15 regardless

The script should also be put inside the Chest if you haven’t done so already

Here let me make a quick video of what i’m talking about.

Avatar is R6, settings have it on R6 and standard animation. I’m confused

This also didn’t work for some reason. My studio might be bugged :thinking:

Alright it did work, after some fixing. Tysm! :smiley: :+1: