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.
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!
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
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)