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
local 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 -- закончили движениеend
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button2Down:Connect(function() if not moving then waypoint = mouse.Hit.Position
-- создаем коробку в целевой точке для поиска пути local part = Instance.new("Part") part.Position = Vector3.new(waypoint.X, waypoint.Y, waypoint.Z) part.Size = Vector3.new(1, 1, 1) part.Transparency = 1 part.CanCollide = false part.Anchored = true part.Parent = workspace waypoint = part moving = true moveToWaypoint() endend)
game:GetService("UserInputService").InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.S then -- прекращаем движение waypoint = nil end end) роблокс выдает оишбку на строке 35: attempt to index nil with 'Destroy'
- 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:local 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.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.