роблокс дал ошибку, найди и исправь local Players = game:GetService("Players") local…
- User
роблокс дал ошибку, найди и исправь local Players = game:GetService("Players") local PathfindingService = game:GetService("PathfindingService") local TweenService = game:GetService("TweenService")
-- Здесь укажите дистанцию до игрока на которой NPC должен остановиться local desiredDistance = 10
-- Здесь укажите время, которое NPC будет смотреть на игрока local watchTime = 3
-- Здесь укажите скорость NPC при убегании local runSpeed = 20
-- Здесь укажите скорость NPC при атаке local attackSpeed = 25
local npc = script.Parent local humanoid = npc:WaitForChild("Humanoid")
local function getNearestPlayer() local minDistance = math.huge local nearestPlayer = nil
for _, player in ipairs(Players:GetPlayers()) do if player.Character and player.Character:FindFirstChild("Humanoid") then local distance = (player.Character.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).magnitude if distance < minDistance then minDistance = distance nearestPlayer = player.Character.HumanoidRootPart end end end return nearestPlayerend
local function attackPlayer() humanoid.WalkSpeed = attackSpeed npc:WaitForChild("Head").BrickColor = BrickColor.new(255, 0, 0)
-- Здесь добавьте свой код для атаки игрока wait(watchTime) humanoid.WalkSpeed = runSpeed npc:WaitForChild("Head").BrickColor = BrickColor.new(0, 0, 255)end
local function runAway() humanoid.WalkSpeed = runSpeed local direction = (npc.Position - nearestPlayer.Position).unit local runAwayPosition = npc.Position + direction * desiredDistance
local path = PathfindingService:CreatePath() path:ComputeAsync(npc.Position, runAwayPosition) local points = path:GetWaypoints() for _, point in ipairs(points) do local tweenInfo = TweenInfo.new((point.Position - npc.Position).magnitude / runSpeed, Enum.EasingStyle.Linear) local tween = TweenService:Create(npc, tweenInfo, {Position = point.Position}) tween:Play() tween.Completed:Wait() end humanoid.WalkSpeed = 0end
while true do local nearestPlayer = getNearestPlayer()
if nearestPlayer then local distance = (nearestPlayer.Position - npc.HumanoidRootPart.Position).magnitude if distance <= desiredDistance then if npc:IsInLineOfSight(nearestPlayer) then humanoid:MoveTo(nearestPlayer.Position) humanoid.WalkSpeed = 0 wait(watchTime) if humanoid.Sit == false then humanoid.WalkSpeed = runSpeed end end else if humanoid.WalkSpeed ~= runSpeed then humanoid.WalkSpeed = runSpeed end humanoid:MoveTo(nearestPlayer.Position) end humanoid:MoveTo(nearestPlayer.Position) humanoid.MoveToFinished:Wait() -- Если игрок посмотрел на NPC, NPC должен убежать local direction = (nearesPlayer.Position - npc.HumanoidRootPart.Position).unit local lookDirection = npc:WaitForChild("Head").CFrame.lookVector local dotProduct = lookDirection:Dot(direction) if dotProduct > 0 then runAway() else attackPlayer() end end wait(0.1) - Assistant
Ошибка возникает на строке 94, где пытается получиться позиция игрока (nearestPlayer.Position). Проблема в том, что переменная nearestPlayer не была определена в данном контексте.
Чтобы исправить эту ошибку, нужно определить переменную nearestPlayer перед циклом while, в котором она используется. Можно сделать это следующим образом:
local nearestPlayer = nil
while true do nearestPlayer = getNearestPlayer()
... -- остальной код ... wait(0.1)end