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

How would I play multiple animations with one script to different NPCs?

here is what I have so far

local NPC = game.Workspace.NPC

local function AnimationFunction()
	if NPC:FindFirstChild("StatueAnimation") then
		local animation = NPC:WaitForChild('StatueAnimation')
		local humanoid = NPC:WaitForChild('Humanoid')
		local StatueAnimation = humanoid:LoadAnimation(animation)
		StatueAnimation:Play()
		print("exampleanimation played")
elseif NPC:FindFirstChild("ExampleAnimation") then
			local animation = NPC:WaitForChild('ExampleAnimation')
			local humanoid = NPC:WaitForChild('Humanoid')
		local ExampleAnimation = humanoid:LoadAnimation(animation)
		ExampleAnimation:Play()
		print("exampleanimation played")
		else print("not working")
	end 
	end


for k,n in pairs(game.Workspace:GetChildren()) do
	
	
	if n.Name == "NPC" then
		AnimationFunction()
		
	end
	
end

1 answer

✓ Accepted answer

im PRETTY SURE this will work. If it doesnt, im sorry.

local function AnimationFunction(NPC)
    	if NPC:FindFirstChild("StatueAnimation") then
        	local animation = NPC:WaitForChild('StatueAnimation')
        	local humanoid = NPC:WaitForChild('Humanoid')
        	local StatueAnimation = humanoid:LoadAnimation(animation)
        	StatueAnimation:Play()
        	print("exampleanimation played")
	elseif NPC:FindFirstChild("ExampleAnimation") then
            	local animation = NPC:WaitForChild('ExampleAnimation')
            	local humanoid = NPC:WaitForChild('Humanoid')
        	local ExampleAnimation = humanoid:LoadAnimation(animation)
        	ExampleAnimation:Play()
        	print("exampleanimation played")
        else
	warn("not working")
    end 
end


for k,n in pairs(game.Workspace:GetChildren()) do
	 if n.Name == "NPC" then
        	AnimationFunction(n)
   	end
end

also sorry for horrid formatting

1 comment

0

this did work, Thank you. And no, your code is very clean looking, much better than mine!

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