Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PathfindingService = game:GetService("PathfindingService")
- local pirate = script.Parent
- local humanoid = pirate:WaitForChild("Humanoid")
- local rootPart = pirate:WaitForChild("HumanoidRootPart")
- local npcFolder = workspace:WaitForChild("NPC")
- local dataFolder = workspace:WaitForChild("Data")
- local walkSpeedValue = npcFolder:WaitForChild("WalkSpeed")
- local playerValue = dataFolder:WaitForChild("Player")
- local ATTACK_DISTANCE = 5
- local UPDATE_TIME = 0.5
- pirate:SetAttribute("IsAttacking", false)
- while pirate.Parent do
- humanoid.WalkSpeed = walkSpeedValue.Value
- -- Если пират сейчас атакует, движение останавливаем
- if pirate:GetAttribute("IsAttacking") then
- humanoid:MoveTo(rootPart.Position)
- task.wait(0.1)
- continue
- end
- local player = playerValue.Value
- if not player then
- task.wait(UPDATE_TIME)
- continue
- end
- local character = player.Character
- if not character then
- task.wait(UPDATE_TIME)
- continue
- end
- local targetHumanoid = character:FindFirstChildOfClass("Humanoid")
- local targetRoot = character:FindFirstChild("HumanoidRootPart")
- if not targetHumanoid or not targetRoot or targetHumanoid.Health <= 0 then
- task.wait(UPDATE_TIME)
- continue
- end
- local distance = (targetRoot.Position - rootPart.Position).Magnitude
- -- Если уже достаточно близко, стоим на месте.
- -- Дальше атакой займется PirateAttack.
- if distance <= ATTACK_DISTANCE then
- humanoid:MoveTo(rootPart.Position)
- task.wait(0.1)
- continue
- end
- -- Создаем путь к игроку
- local path = PathfindingService:CreatePath({
- AgentRadius = 2,
- AgentHeight = 5,
- AgentCanJump = true,
- WaypointSpacing = 3
- })
- local success = pcall(function()
- path:ComputeAsync(rootPart.Position, targetRoot.Position)
- end)
- if success and path.Status == Enum.PathStatus.Success then
- local waypoints = path:GetWaypoints()
- for _, waypoint in ipairs(waypoints) do
- if pirate:GetAttribute("IsAttacking") then
- break
- end
- if not playerValue.Value then
- break
- end
- character = playerValue.Value.Character
- if not character then
- break
- end
- targetRoot = character:FindFirstChild("HumanoidRootPart")
- targetHumanoid = character:FindFirstChildOfClass("Humanoid")
- if not targetRoot or not targetHumanoid or targetHumanoid.Health <= 0 then
- break
- end
- distance = (targetRoot.Position - rootPart.Position).Magnitude
- if distance <= ATTACK_DISTANCE then
- break
- end
- if waypoint.Action == Enum.PathWaypointAction.Jump then
- humanoid.Jump = true
- end
- humanoid:MoveTo(waypoint.Position)
- local reached = humanoid.MoveToFinished:Wait()
- if not reached then
- break
- end
- end
- else
- -- Если путь построить не получилось,
- -- просто пытаемся идти прямо к игроку.
- humanoid:MoveTo(targetRoot.Position)
- end
- task.wait(UPDATE_TIME)
- end
Advertisement
Add Comment
Please, Sign In to add comment