I want to play an animation when the player touches a part

Greetings,
A few days ago I decided to make a Halloween game for my boss, I managed to build the houses and the rest and I paid someone to make some trick or treat animations, my only problem is that the animation can’t be played. I tried the script below and there is an error in the output. I don’t understand what is wrong. I need someone to help me fix this and make the animation play. Thanks!

function onTouched(hit)
	local human = hit.Parent:findFirstChild("Humanoid")
	if human then
	local anim = human:LoadAnimation(script.Parent.Animation)
	anim:Play()
	end
end
script.Parent.Touched:connect(onTouched)

Screenshot 2022-09-25 215215

1 Like

You need to get the animator (a child of the humanoid) before loading an animation.

This explains it quite well.

And how do I get that? Can you explain?

Assuming you’ve worked out how to do it, if you’ve marked the post as solved?

Just checking - in case you still need help!

I still need some help with the code… You can help me with that?

Sure.

An animator is a child of the humanoid, you should check for that first.

-- Finds the humanoid
local humanoid = char:FindFirstChildOfClass("Humanoid")

-- Finds the animator
local animator = humanoid:FindFirstChildOfClass("Animator")

-- Checks whether the animator exists
if animator == true then

    -- Declares a new variable,where we'll load the animation
    local anim = animator:LoadAnimation(your_animation)

    -- Plays the animation
    anim:Play()
end

Hope that clarifies? A better example can be found via the link I posted, at the bottom of the page.

1 Like

I have just 2 more questions, in the (your_animation) I need to put the ID of the animation or what?
And when I copied this code and I put it in the script the char appears with a underline. What should I do?

You did script.Parent, which means the object above script . Remove .Parent from the animation load variable.

1 Like

The code I posted works on the assumption that it’s an edited version of your original code.

The your_animation variable should be a reference to whatever your animation file is :slight_smile:

Turn into

local anim = human:LoadAnimation(script.Animation)
1 Like

Is this good?

local animation = script.Animation
-- Finds the humanoid
local humanoid = char:FindFirstChildOfClass("Humanoid")

-- Finds the animator
local animator = humanoid:FindFirstChildOfClass("Animator")

-- Checks whether the animator exists
if animator == true then

	-- Declares a new variable,where we'll load the animation
	local anim = animator:LoadAnimation(animation)

	-- Plays the animation
	anim:Play()
end
-- Declares a function
local function onTouched(partTouched)
    
    -- Defines a variable pointing to your animation
    local animation = script.Animation

    -- Finds the humanoid, fails: exits the function
    local humanoid = partTouched.Parent:FindFirstChildOfClass("Humanoid") or return

    -- Finds the animator, fails: exits the function
    local animator = humanoid:FindFirstChildOfClass("Animator") or return

    -- Loads the animation
    local anim = animator:LoadAnimation(animation)

    -- Plays the animation
    anim:Play()
end

-- Calls the above function when the player touches the part
script.Parent.Touched:Connect(onTouched)

And if I pase this code in the script it should work or I need to make some changes?

Animation is not a valid member of Workspace.Animation, because it is a valid member of Workspace.Animation["Animations Script"]. Change the local anim = human:LoadAnimation(script.Parent.Animation) to local anim = human:LoadAnimation(script.Animation).

It doesn’t work. I tried it and nothing happens…

I tried this too! No errors in the output and it still not works.

1 Like

It doesn’t work… I don’t know what to do!

Is it errorless now? (Gotta add more letterss aaaaa)

Touched is not a valid member of Script “Workspace.Animation.Animations Script”

Now you’re trying to do the .Touched function on the script (At least that’s how it sounds like)