How would I make an animation play for an npc if a player touches a part?

This seems so simple but yet I can’t figure it out. I believe I’m overthinking this but I have no one to ask to help me with this. Basically, I’ve been trying to create a “horror” game but I don’t think that just a gui changing its visibility is scary enough. To improve this I wanted to make a touch part that would play an animation on a humanoid that would basically play a 3d jumpscare animation. If anyone could help me out or lead me in the correct direction it would be highly appreciated, thanks.

2 Likes

This should work:

local part = -- path to part
local animId = -- animation id

part.Touched:Connect(function(hit)
   local hum = hit.Parent:FindFirstChild("Humanoid")
   if hum then
      local Animation = Instance.new("Animation")
      Animation.Parent = hum
      Animation.AnimationId = animId
      local animTrack = hum:LoadAnimation(Animation)
      animTrack:Play()
   end
end
2 Likes

image

It errors and it doesn’t create a new animation inside of the dummy.

Why is this so difficult T_T

@oofblocks was correct with his answer, except he just forgot to add a colon at the end to finish it.

Here’s the correct code.

local part = -- path to part
local animId = -- animation id

part.Touched:Connect(function(hit)
   local hum = hit.Parent:FindFirstChild("Humanoid")
   if hum then
      local Animation = Instance.new("Animation")
      Animation.Parent = hum
      Animation.AnimationId = animId
      local animTrack = hum:LoadAnimation(Animation)
      animTrack:Play()
   end
end) -- colon missing here

That will make the player play the animation, not the NPC. Something like this might work:

wait(4)
local part = -- path to part
local animId = -- animation id
local NPC =  --path to NPC model
 local Animation = Instance.new("Animation")  
Animation.Parent = NPC:WaitForChild("Humanoid")
Animation.AnimationId = animId

local db = false
part.Touched:Connect(function(hit)
   local hum = hit.Parent:FindFirstChild("Humanoid")
   if hum  and db == false then
      db = true
      local animTrack = NPC.Humanoid:LoadAnimation(Animation)
      animTrack:Play()
      wait(5)
      db = false
   end
end)
2 Likes

The code above works perfectly fine, at least it should. This code is longer and unnecessary. @oofblocks suggestion was perfectly valid, just had a small mistake.

Oh, I thought that he wanted an NPC to jump out, I didn’t realize that he wanted there player to play the animation. Sorry about that.

No, you’re correct thanks for your feedback

This is leading in the correct path but I’m still having issues with the script and I’m not sure how to fix them

image

Sorry for the late reply, I was in school. It sounds like the animation id is incorrect, did you format it something like this (replace the numbers with your id)

rbxassetid://123456789

I had put the ID itself in, I will try the rbxassetid thanks

It worked for me thank you lol

This is what I ended up with

wait(4)
	local part = script.Parent-- path to part
	local NPC =  script.Parent.Dad--path to NPC model
	local Animation = Instance.new("Animation")  
Animation.Parent = NPC:WaitForChild("Humanoid")
Animation.AnimationId = "rbxassetid://6446931879"

local db = false
part.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum  and db == false then
		db = true
		local animTrack = NPC.Humanoid:LoadAnimation(Animation)
		animTrack:Play()
		wait(5)
		db = false
	end
end)