Рустам Абдрашитов
Мыслитель
(8566)
1 месяц назад
local monster = script.Parent
local pathfindingService = game:GetService("PathfindingService")
local players = game:GetService("Players")
function findNearestPlayer()
local closestPlayer, closestDistance = nil, math.huge
for _, player in pairs(players:GetPlayers()) do
local humanoidRootPart = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local distance = (monster.HumanoidRootPart.Position - humanoidRootPart.Position).magnitude
if distance < closestDistance then
closestDistance = distance
closestPlayer = humanoidRootPart
end
end
end
return closestPlayer
end
function moveToTarget()
while true do
local targetPlayer = findNearestPlayer()
if targetPlayer then
local path = pathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentJumpHeight = 10,
AgentMaxSlope = 45,
})
path:ComputeAsync(monster.HumanoidRootPart.Position, targetPlayer.Position)
path:MoveTo(monster)
path:MoveToFinished:Wait()
end
wait(1) -- Проверка каждые 1 секунду
end
end
moveToTarget()