How do i make a player play an animation when a function is activated?

Hey guys, im trying to work out an animation using Moon Animator. Ive got the ID already but how do i make a player, that your playing as, play an animation when a proximity prompt is triggered

2 Likes

Hi!

First create a ProximityPrompt and place it under the rig you wish to use. Create the following within the ProximityPrompt:

image

Inside your script you should have this.

script.Parent.Triggered:Connect(function(Player) 

	local plr = Player
	local chr = plr.Character
	local hum = chr:WaitForChild("Humanoid")
	local animator = hum:WaitForChild("Animator")

	local animation = script:WaitForChild("Animation") --This is your animation
	local animate = animator:LoadAnimation(animation) -- This loads your animation

	animate:Play() --This plays the loaded animation
	
end)
4 Likes

Thanks! it works! By the way is there also a way to set them in a custom position and not make them walk?

2 Likes

Simply add this line of code in your script:

script.Parent.Triggered:Connect(function(Player)

	local plr = Player
	local chr = plr.Character
	local hum = chr:WaitForChild("Humanoid")
	local animator = hum:WaitForChild("Animator")

	local animation = script:WaitForChild("Animation")
	local animate = animator:LoadAnimation(animation)
	
	hum.WalkSpeed = 0 -- This line here
	animate:Play()

end)
1 Like

Thanks! Could i also set a position for the character?

1 Like

I also added a way to revert the walkspeed back to normal after your animation is done playing.

script.Parent.Triggered:Connect(function(Player)

	local plr = Player
	local chr = plr.Character
	local hum = chr:WaitForChild("Humanoid") 
	local humrp = Player.Character:WaitForChild("HumanoidRootPart") --Add this line
	local animator = hum:WaitForChild("Animator")

	local animation = script:WaitForChild("Animation")
	local animate = animator:LoadAnimation(animation)
	
	hum.WalkSpeed = 0
	humrp.CFrame = script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(0,0,0) --Add this line and change the Z axis to fix them in a position in front of your NPC.
	animate:Play()
	task.wait() 
	hum.WalkSpeed = 16

end)
1 Like

Hey, its not working but its giving me the error saying “HumanoidRootPart” is not a part of Workspace.Part"

Also, how exactly should you change the CFrame Value for your rig

1 Like

Please send me your code so we can work on the first issue.

1 Like
script.Parent.Triggered:Connect(function(Player)

	local plr = Player
	local chr = plr.Character
	local hum = chr:WaitForChild("Humanoid")
	local humrp = Player.Character:WaitForChild("HumanoidRootPart")
	local animator = hum:WaitForChild("Animator")

	local animation = script:WaitForChild("Animation")
	local animate = animator:LoadAnimation(animation)

	hum.WalkSpeed = 0 -- This line here

	animate:Play()
	humrp.CFrame = script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(0,0,4) --Add this line and change the Z axis to fix them in a position in front of your NPC.
	script.Parent.Enabled = false
	wait(10)
	animate:Stop()
	hum.Walkspeed = 16

end)
1 Like

And can you send a screenshot of the error aswell.

1 Like

1 Like

Where do you have your ProximityPrompt in explorer?

1 Like

The part i click plays the animation
image

2 Likes

Okay, the reason for this is because I assumed you were using an NPC for your ProximityPrompt.

The reason it isn’t working for you is because a HumanoidRootPart can only belong to something that contains a Humanoid, which a part doesnt.

Okay, so what do i do now?
////

1 Like

Use the scrolling bar on my last response, I added a line in there that shows you what to change.

What line? i dont see any difference.
/////

1 Like

Change to

humrp.CFrame = script.Parent.Parent.CFrame * CFrame.new(0,0,4)
2 Likes

Wait one more thing, the whole walkspeed thing wont work anymore

Are there any errors? What is happening when you play it?

1 Like