How could I make my NPC play an animation when I click on it?

Hi, I know nothing about scripting so help would be appreciated. :slight_smile:

  1. What do you want to achieve?
    I want my NPC to play an animation when I click on it then animation stops after the animation stops playing then when I click on it again it will play the animation again.

  2. What is the issue?
    I don’t have an issue about this to be honest.

  3. What solutions have you tried so far?
    I tried looking at the dev forum but I can’t seem to find topics similar to mine same goes for YouTube.

3 Likes

try adding ClickDetector on the torso or upper torso of whatever on the NPC
and add a script inside the click detector. now add animation on the script

and then edit the script and should be like:

local NPC = script.Parent.Parent.Parent -- the npc
local Humanoid = NPC:FindFirstChild("Humanoid") -- the npc's humanoid
local Animation = script:WaitForChild("Animation") -- the animation

script.Parent.MouseClick:Connect(function()
    if Humanoid ~= nil then
        Humanoid:LoadAnimation(Animation)
    end
end)

dont know if this really works though… i directly typed this here

This should be in Help and Feedback:Scripting Support

Alright, sorry. (30 charrssssss)

1 Like

It didn’t work. (30 charsssss)

here. i just made it because too lazy to explain.

NOTE: make sure you change the animation id

image
image

https://www.roblox.com/library/5508523855/ClickNPC-Thingy


hope you understand how it works

Will this work if I use it on an r15 NPC?

yes of course. its in the rig type your using

this can make the npc repeatedly play the animation. this is better:

local isPlaying = false
local animTime = 10 -- replace with how long the animation is
local NPC = script.Parent:WaitForChild("NPC")
local Hum = NPC:FindFirstChild("Humanoid")
local Anim = script:WaitForChild("Animation")
local DetectorPart = script.Parent:WaitForChild("ClickPart")
local ClickDetector = DetectorPart.ClickDetector
local LoadAnim = Hum:LoadAnimation(Anim)
ClickDetector.MouseClick:Connect(function()
	if isPlaying == true then
		print("Already Playing")
	elseif isPlaying == false then
		isPlaying = true
		LoadAnim:Play()
		wait(animTime)
		isPlaying = false
	end
end)

It did not work, I’m not sure what I’m doing wrong. ;-;

change the name to NPC 30 charsssss

does your rig type match the NPC’s rig type? like is your animation r15 and the NPC is r6? or the opposite? did you change the animation id?

I did. (30 charssssssssssssss)

Yes, my animation is r15 and I also changed the animation id

any errors?
30 charactersssssss


This is the output right now

Bam. Error
Infinite yield means it can wait forever
it think that the clickdetector is parented to itsself
make sure you add an extra .Parent to the clickdetector variable

oof! the rig type of the model i sent is r6. try to use the r15 rig here.

https://www.roblox.com/library/5508580295/R6-And-R15-Click-To-Play-Animation


Animations needs to match the rig your using

Script. That isn’t the problem.
She has to locate the click detector correctly, as you can see from the output

Like this?

local DetectorPart = script.Parent.Parent:WaitForChild(“ClickPart”)