Hello developers, I am making a Story game, I have made an animation and the script to make a NPC go to the part. However, I don’t know how to make it activate the walk animation if anyone can help me, I would highly appreciate it if anyone could help me!
Here is the script if anyone needs it;
-- get pathfinding service
local pathfindingService = game:GetService("PathfindingService")
-- Variables for NPC humanoid, torso, and destination
local humanoid = script.Parent.Humanoid
local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("Torso")
local destination = game.Workspace.Destination.Position
-- create path object
local path = pathfindingService:CreatePath()
-- compute a path
path:ComputeAsync(body.Position, destination)
-- get the waypoints table
local waypoints = path:GetWaypoints()
-- iterate through all waypoints, and jump when necessary
for k, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
-- change humanoid state to jump if necessary
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid.MoveToFinished:Wait()
end
And this is my workspace!
I would recommend reading up on Animations within games in the article below:
Using Animations in Games
Basically, you’ll want to get the Humanoid of the NPC and use Humanoid:LoadAnimation
(preferably Animator:LoadAnimation
since the Humanoid counterpart is deprecated), and load the animation in and play it.
I hope this helps!
I found this script which I would like to use, however it doesn’t explain where to put the script inside of the rig.
local rig = script.Parent
-- Destroy the humanoid object if it exists
local humanoid = rig:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:Destroy()
end
-- Create new "Animation" instance
local kickAnimation = Instance.new("Animation")
-- Set its "AnimationId" to the corresponding animation asset ID
kickAnimation.AnimationId = "rbxassetid://2515090838"
-- Create a new "AnimationController" and "Animator"
local animController = Instance.new("AnimationController")
animController.Parent = rig
local animator = Instance.new("Animator")
animator.Parent = animController
-- Load animation onto the animator
local kickAnimationTrack = animator:LoadAnimation(kickAnimation)
-- Play animation track
kickAnimationTrack:Play()
-- If a named event was defined for the animation, connect it to "GetMarkerReachedSignal"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)