Animations not playing when :Play() is called

I’ve been trying to make a script that has the player do an eat animation when they touch a heal item or something like that. I have not been able to achieve this though, even though it was working earlier, and now it just doesn’t. I’ve tried to republish the animation multiple times, and I have checked to make sure it was Action1 or above. But I still have yet to succeed.

Here is the code.

local tool = script.Parent

tool.Activated:Connect(function()
	local anim = Instance.new("Animation",script)
	anim.AnimationId = "rbxassetid://135867640207159"
	local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
	local animator:Animator = character.Humanoid.Animator
	local playanim = animator:LoadAnimation(anim)
	task.wait(.15)
	playanim:Play()
end)

I’m currently writing this at night (in my time) and the code may be a bit bad due to how late it is for me.

So few things, you don’t need to create a new animation instance every time the player clicks the tool. Also, you should avoid using the parent argument of Instance.new, read more here.

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

local Animation = Instance.new("Animation")
Animation.Parent = script.Parent
Animation.AnimationId = "rbxassetid://135867640207159"

local Animator = Character:WaitForChild("Humanoid").Animator
local AnimationTrack = Animator:LoadAnimation(Animation)

script.Parent.Activated:Connect(function()
	AnimationTrack:Play()
	print("Played")
end)
3 Likes

Thanks a bunch! It worked, I’ll be sure to avoid using the parent argument from now on.

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