RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"only move to the door if the nearest waypoint is 20 and the npc has already moved to 20 before"
-- server script local pfs = game:GetService("PathfindingService") local dummy = script.Parent local waypoints = script.Parent.Parent.Parent.Waypoints:GetChildren() -- List of waypoints local door = game.Workspace.Door local doorRange = 20 local attackRange = 5 local currentWaypointIndex = 1 local hasMovedTo20 = false -- Function to attack the door local function attackDoor() dummy.Humanoid:MoveTo(door.Position) if (door.Position - dummy.HumanoidRootPart.Position).Magnitude < attackRange then local attackAnimation = dummy.Humanoid:LoadAnimation(dummy.Animations.AttackAnimation) attackAnimation:Play() print("NPC is attacking the door!") -- log the attack event end end -- Function to move to the next waypoint or door local function moveToNextWaypointOrDoor() while true do local nearestWaypointIndex = 1 local nearestWaypointDistance = (waypoints[1].Position - dummy.HumanoidRootPart.Position).Magnitude for i=2, #waypoints do local distance = (waypoints[i].Position - dummy.HumanoidRootPart.Position).Magnitude if distance < nearestWaypointDistance then nearestWaypointIndex = i nearestWaypointDistance = distance end end currentWaypointIndex = nearestWaypointIndex if (waypoints[currentWaypointIndex].Position - dummy.HumanoidRootPart.Position).Magnitude < 1 then currentWaypointIndex = currentWaypointIndex % #waypoints + 1 end print("Moving to waypoint: " .. waypoints[currentWaypointIndex].Name .. " at position: " .. tostring(waypoints[currentWaypointIndex].Position)) -- log the next waypoint local path = pfs:CreatePath() path:ComputeAsync(dummy.HumanoidRootPart.Position, waypoints[currentWaypointIndex].Position) for _, waypoint in pairs(path:GetWaypoints()) do dummy.Humanoid:MoveTo(waypoint.Position) dummy.Humanoid.MoveToFinished:Wait() end if currentWaypointIndex == 20 then hasMovedTo20 = true end if currentWaypointIndex == #waypoints and hasMovedTo20 then dummy.Humanoid:MoveTo(door.Position) end task.wait(1) end end -- Start the NPC's behavior spawn(moveToNextWaypointOrDoor)