You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pathfinding寻路功能失效求助:Roblox新手脚本开发问题

寻路功能失效问题排查

我是脚本开发新手,目前遇到Pathfinding寻路功能失效的问题,十分困惑。我能理解自己编写的其他代码,也查阅了相关文档,但在整合寻路功能、让NPC仅在定位到最近玩家后才生成路径并沿其移动这一环节上陷入了困境。原本我以为只需在判断玩家距离足够近的if语句内声明路径,NPC就能沿路径移动,之后参考教程使用了GetWayPoints()方法,但依旧没有效果。

以下是我最新尝试的代码:

local runService = game:GetService("RunService") -- Run Service sort of like unitys Update frame by frame, SEE DOCUMENTATION SAVED FOLDER
local players = game:GetService("Players") -- Players will help "get" the players in the game

local humanoid = script.Parent -- grabs the parent of the script which is humanoid
local root = humanoid.Parent.PrimaryPart --root is the humanoids parent i.e models primary part which is typically the humanoid root part

local PathfindingService = game:GetService("PathfindingService"); -- A path finding service



local wantedDistance = 30 --  How far he can search or should be trying to search, The value now is small testing needed
local stopDistance = 5 -- In caase we want to use this to make him stop (like if he had a kill radius instead of touching)

local damage = 50
local attackDistance = 8
local attackWait = 1
local lastAttack = tick()
function findNearestPlaya()
    local playerList = players:GetPlayers()
    
    local playerNearest = nil
    local dist = nil
    local direction = nil
    
    for _, player in pairs(playerList) do -- basically a for each loop that says for each player in the list of players
        local character = player.Character 
        if character  then -- will only eggsacute if a player/character exists
            local distanceV = player.Character.HumanoidRootPart.Position - root.Position -- Distance  ''Vector'' equals the distance from the player torso/Root position minus the distance of the models primary part which we have as root
            
        
        
            
            if not playerNearest then
                playerNearest = player

                dist = distanceV.Magnitude -- distance vector magnitude gives us the actual distance
                direction = distanceV.Unit -- direction of the nearest player, SEE DOCUMENTATIOIN FOR UNIT AND MAGNITUDE

            elseif distanceV.Magnitude < dist then -- If the player is closer than the set nearest player then ''replace'' the player

                playerNearest = player -- resets to new player
                dist = distanceV.Magnitude
                direction = distanceV.Unit

            end
        end
    
    end
    
    return playerNearest, dist, direction -- function so return which playa and his distance and direction
end

-- Another call from the runService class that runs every "physics frame", Which I think is like the updateBefore in unity
runService.Heartbeat:Connect(function() -- lua thing essentially this odd function call thing is just an anonymous function meaning it will execute every heartbeat
local path = PathfindingService:CreatePath()
local playerNearest, distance, direction = findNearestPlaya()

if the distance is within range of the wanted distance
if playerNearest  then
if distance <=wantedDistance and distance >= stopDistance then
path:ComputeAsync(humanoid.PrimaryPart.Position, playerNearest.PrimaryPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints)  do
humanoid.MoveTo(waypoint.Position)
end

else 
humanoid:Move(Vector3.new()) -- 


end

if distance <= attackDistance and tick() - lastAttack >= attackWait then
lastAttack = tick();
playerNearest.Character.Humanoid.Health -= damage
end
end


end)

我原本预期NPC会计算出到最近玩家的路径并移动,但实际并未生效,无法理解其中原因。


内容的提问来源于stack exchange,提问作者userr554

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 01:58:24