Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make it so when a player touches a certain part,an animation is played on an NPC?

I want to make it so when I touch a certain part,an animation plays in a certain npc. I found a script on the web but I don't know how to use it or if it is right. Btw I'm terrible at scripting.

local part = --write here
local animation = --write here
local animationTrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)

part.Touched:connect(function(p)
    if game.Players:GetPlayerFromCharacter(p.Parent) = game.Players.LocalPlayer then
        animationTrack:Play()
        --add more code
    end
end)

1 answer

✓ Accepted answer

First: Create a Part and an Animation in your workspace.

Replace your script for this(Its like the one you posted but it works for NPCs(Yours Works for players))


local part = game.Workspace.Part
local animation = game.Workspace.Animation
animation.AnimationId = "http://roblox.com/Asset?ID=467504759"

part.Touched:Connect(function()
 local animationTrack = game.Workspace.NPC.Humanoid:LoadAnimation(animation)
 animationTrack:Play()
end)

You can change the animation ID at line 3 for the ID of the animation of your choice(I made one as a sample, you can use it if you want to)

You can replace the word "NPC" at line 6 for the name of your NPC.

Now just touch the part.Here ill send some results:

Before touching it: http://prntscr.com/qdecl5

After touching it(Couldn't make a video sry): http://prntscr.com/qded0p

Contact me if you have any issues with the script.

7 comments

0

Thank you very much! If you have time, how do I make it debounce?

0

Im sorry i dont get it, what exactly do you mean with debounce?

0

It like glitches out when I move on the block

0

Oh, now i get it.Try replacing this script with the one i sent you: https://pastebin.com/UCbNh3c8 You can change the number at line 12, it is the amount of time you have to wait until you can make the npc do the animation again.

0

Thank you man, very much appreciated.

0

No problem, Have a nice day!

This discussion is preserved as it was. New answers, comments, and votes are closed.