-- NPC Pathfinding Script (Server-Side) -- Place in NPC local PathfindingService = game:GetService("PathfindingService") local npc = script.Parent local humanoid = npc:WaitForChild("Humanoid") local humanoidRootPart = npc:WaitForChild("HumanoidRootPart") local destination = workspace.Target -- Reference the Animate script local animateScript = npc:WaitForChild("Animate") -- Access the built-in walk animation local walkAnimationTrack = humanoid:LoadAnimation(animateScript.walk.WalkAnim) local function moveToTarget(targetPosition) -- Create a path to the target local path = PathfindingService:CreatePath({ AgentRadius = 2, AgentHeight = 1, AgentCanJump = true, AgentJumpHeight = 5, AgentMaxSlope = 45 }) -- Compute the path to the target path:ComputeAsync(humanoidRootPart.Position, targetPosition.Position) -- Check if the path was successfully computed if path.Status == Enum.PathStatus.Success then local waypoints = path:GetWaypoints() -- Play walk animation when NPC starts moving walkAnimationTrack:Play() -- Move the NPC along the waypoints for _, waypoint in ipairs(waypoints) do humanoid:MoveTo(waypoint.Position) humanoid.MoveToFinished:Wait() end -- Stop the walk animation when NPC reaches the destination walkAnimationTrack:Stop() else print("No path found!") end end moveToTarget(destination)
Project File Here (FREE):
www.patreon.com/posts/roblox-studio-ai-111804587?Link&
-- NPC Pathfinding Script (Server-Side) -- Place in NPC
local PathfindingService = game:GetService("PathfindingService")
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local humanoidRootPart = npc:WaitForChild("HumanoidRootPart")
local destination = workspace.Target
-- Reference the Animate script
local animateScript = npc:WaitForChild("Animate")
-- Access the built-in walk animation
local walkAnimationTrack = humanoid:LoadAnimation(animateScript.walk.WalkAnim)
local function moveToTarget(targetPosition)
-- Create a path to the target
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 1,
AgentCanJump = true,
AgentJumpHeight = 5,
AgentMaxSlope = 45
})
-- Compute the path to the target
path:ComputeAsync(humanoidRootPart.Position, targetPosition.Position)
-- Check if the path was successfully computed
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
-- Play walk animation when NPC starts moving
walkAnimationTrack:Play()
-- Move the NPC along the waypoints
for _, waypoint in ipairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
-- Stop the walk animation when NPC reaches the destination
walkAnimationTrack:Stop()
else
print("No path found!")
end
end
moveToTarget(destination)
could you make a enemy attacking ai, but instead of just scripting it explain how it works
감사합니다