I am working on this script to make an NPC play a random animation every time it is clicked. I got it to work with 1 animation but am doing something wrong for random anim, I could not figure out on my own so here I am, welp! (The image shows the items locaitons, I wonder if I am calling something wrong…)
local NPC = script.Parent:WaitForChild("NPC")
local Hum = NPC:FindFirstChild("Humanoid")
local DetectorPart = script.Parent:WaitForChild("ClickPart")
local ClickDetector = DetectorPart.ClickDetector
local Music = script:WaitForChild("Music")
local anim = nil
local whichanim = math.random(1,3)
ClickDetector.MouseClick:Connect(function()
anim = script.Parent:FindFirstChild("Humanoid").Animator:LoadAnimation(script["Animation"..whichanim])
anim:Play()
print("Played")
Music:Play()
wait(5)
Music:Stop()
end)
Alright so here’s what I personally like to do, but its up to you on how you do it -
I would first go to the NPC and place in the ‘Animation’ Objects and place their AnimationIDs in them. And name them distinctly. And then place a click detector inside the Torso of the NPC.
I would place a Script inside the NPC model.
Then I’ll use this code -
local npc = script.Parent;
local cd = script.Parent.Torso.ClickDetector;
local anims = {"AnimationObjectName1", "AnimationObjectName2", "AnimationObjectName3"}; -- I put the names of the Animation Objects here.
cd.MouseClick:Connect(function()
local animator = Instance.new("Animator", npc:FindFirstChild("Humanoid"));
local chosen = animator:LoadAnimation(npc:FindFirstChild(anims[math.random(1, #anims)]));
chosen:Play();
end);
local NPC = script.Parent:WaitForChild("NPC")
local Hum = NPC:FindFirstChild("Humanoid")
local DetectorPart = script.Parent:WaitForChild("ClickPart")
local ClickDetector = DetectorPart.ClickDetector
local Music = script:WaitForChild("Music")
local anim = nil
local whichanim = math.random(1,3)
ClickDetector.MouseClick:Connect(function()
anim = script.Parent:FindFirstChild("Humanoid").Animator:LoadAnimation(script["Animation"..tostring(whichanim)])
anim:Play()
print("Played")
Music:Play()
wait(5)
Music:Stop()
end)
I appreciate the answers! sadly I did not managed to make them work, here is your script suggestion implemented. Mememoxine.rbxl (66.6 KB) Jaipack17.rbxl (67.7 KB)
local NPC = script.Parent:WaitForChild("NPC")
local Hum = NPC.Humanoid
local DetectorPart = script.Parent.ClickPart
local ClickDetector = DetectorPart.ClickDetector
local Music = script:WaitForChild("Music")
local anim = nil
ClickDetector.MouseClick:Connect(function()
local whichanim = math.random(1,3)
anim = Hum:LoadAnimation(script["Animation"..tostring(whichanim)])
anim:Play()
print("Played")
Music:Play()
wait(5)
Music:Stop()
anim:Stop()
end)