NPC с патрулированием в Roblox Studio

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 พ.ย. 2024

ความคิดเห็น • 2

  • @blogo-Master
    @blogo-Master หลายเดือนก่อน

    Привет, можешь Скрипт дать? Для скопировки

    • @CodeWithAnton
      @CodeWithAnton  หลายเดือนก่อน

      Лови!
      local Players = game:GetService("Players")
      local folder = script.Parent
      local waypointsFolder = folder.Waypoints
      local waypoints = waypointsFolder:GetChildren()
      local rig = folder.Rig
      local humanoid: Humanoid = rig:FindFirstChildWhichIsA("Humanoid")
      local animateScript = rig:WaitForChild("Animate")
      local runAnimation = animateScript:WaitForChild("run"):WaitForChild("RunAnim")
      local animator = humanoid:WaitForChild("Animator")
      local runTrack: AnimationTrack = animator:LoadAnimation(runAnimation)
      --wait(5)
      runTrack:Play()
      local lastWayPointIndex = 1
      local FOLLOW_DISTANCE = 30
      local MAX_DISTANCE_FROM_LAST_WAYPOINT = 30
      local READY_TO_CHASE = true
      local function findNearestPlayer()
      local nearestPlayer
      local npcPosition = rig.PrimaryPart.Position
      for _, player: Player in pairs(Players:GetPlayers()) do
      local character = player.Character
      if character and character:FindFirstChild("HumanoidRootPart") then
      local distance = (npcPosition - character.HumanoidRootPart.Position).magnitude
      if distance < FOLLOW_DISTANCE then
      nearestPlayer = player
      end
      end
      end
      return nearestPlayer
      end
      local function chasePlayer(player: Player)
      local lastWayPointPosition = waypoints[lastWayPointIndex].Position
      while task.wait(0.01) do
      local distanceToPlayer = (
      player.Character.HumanoidRootPart.Position -
      rig.PrimaryPart.Position
      ).magnitude
      local distanceToLastPosition = (
      lastWayPointPosition - rig.PrimaryPart.Position
      ).magnitude
      if (
      (distanceToPlayer < FOLLOW_DISTANCE) and
      (distanceToLastPosition < MAX_DISTANCE_FROM_LAST_WAYPOINT)
      ) then
      humanoid:MoveTo(player.Character.HumanoidRootPart.Position)
      humanoid.MoveToFinished:Wait()
      else
      READY_TO_CHASE = false
      return
      end
      end
      end
      local function onWayPoint(waypoint: Part)
      print("TEST")
      end
      local function getWaypointByName(index: number)
      for _, waypoint in pairs(waypoints) do
      if waypoint.Name == tostring(index) then
      return waypoint
      end
      end
      return waypoints[1]
      end
      while task.wait(0.1) do
      local nearestPlayer = findNearestPlayer()
      if nearestPlayer and READY_TO_CHASE then
      chasePlayer(nearestPlayer)
      else
      local lastWayPoint = getWaypointByName(lastWayPointIndex)
      spawn(function()
      onWayPoint(lastWayPoint)
      end)
      humanoid:MoveTo(lastWayPoint.Position)
      humanoid.MoveToFinished:Wait()
      lastWayPointIndex = lastWayPointIndex % #waypoints + 1
      READY_TO_CHASE = true
      end
      end