local pathfindingService = game:GetService("PathfindingService") local player =…
- User
local pathfindingService = game:GetService("PathfindingService") local player = game.Players.LocalPlayer
local waypoint = nil -- текущая цель игрока local moving = false -- флаг, определяющий, движется ли игрок в данный момент
local function moveToWaypoint() if not waypoint then return end
codelocal humanoidRootPart = player.Character and player.Character:FindFirstChild("HumanoidRootPart") if not humanoidRootPart then return end local path = pathfindingService:CreatePath() path:ComputeAsync(humanoidRootPart.Position, waypoint.Position) if path.Status == Enum.PathStatus.Success then local waypoints = path:GetWaypoints() for i, waypoint in ipairs(waypoints) do if waypoint.Action == Enum.PathWaypointAction.Jump then -- прыгаем на пути humanoidRootPart.Parent.Humanoid.Jump = true end humanoidRootPart.Parent.Humanoid:MoveTo(waypoint.Position) -- перемещаемся к каждой точке пути humanoidRootPart.Parent.Humanoid.MoveToFinished:Wait() -- ожидаем окончания перемещения к точке end -- добрались до последней точки пути waypoint:Destroy() -- удаляем созданный ранее part waypoint = nil else -- не удалось найти путь, двигаемся к цели напрямую humanoidRootPart.Parent.Humanoid:MoveTo(waypoint.Position) while waypoint and humanoidRootPart.Parent.Humanoid.WalkSpeed ~= 0 do humanoidRootPart.Parent.Humanoid.MoveToFinished:Wait() -- ожидаем окончания перемещения if waypoint then humanoidRootPart.Parent.Humanoid:MoveTo(waypoint.Position) end end -- добрались до цели или была нажата кнопка “S” для прекращения движения waypoint:Destroy() -- удаляем созданный ранее part waypoint = nil end moving = false -- закончили движение - Assistant
The issue occurs because the
waypointvariable is nil when trying to access its Destroy method. This is likely due to thewaypointbeing nil at that point in the code.To fix this issue, you can add a check for
waypointbefore trying to destroy it. Here's the updated code:lualocal function moveToWaypoint() -- ... if path.Status == Enum.PathStatus.Success then -- ... -- добрались до последней точки пути if waypoint then waypoint:Destroy() -- удаляем созданный ранее part waypoint = nil end else -- ... -- добрались до цели или была нажата кнопка “S” для прекращения движения if waypoint then waypoint:Destroy() -- удаляем созданный ранее part waypoint = nil end end moving = false -- закончили движение endBy adding the
if waypoint thencheck before attempting to destroy the waypoint, the error should be resolved.