Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

[SOLVED] The pathfinding works on npc but not on localplayer?

Asked by
imKlevi 94
2 years ago
Edited by imKirda 2 years ago
local PathfindingService = game:GetService("PathfindingService")

local function findPath(player)
    local human = player.Character
    local humanoid = human.Humanoid
    local endDestination = workspace.Obby.WalkToLocation
    local path = PathfindingService:CreatePath()
    path:ComputeAsync(human.HumanoidRootPart.Position, endDestination.Position)
    if path.Status == Enum.PathStatus.Success then
        local Nodes = path:GetWaypoints()

        for i,v in pairs(Nodes) do
            local node = Instance.new("Part", workspace)
            node.Size = Vector3.new(1,1,1)
            node.Position = v.Position
            node.Anchored = true
            node.CanCollide = false
            humanoid.WalkToPoint = v.Position
            humanoid:MoveTo(v.Position)
        end
    end
end

game.ReplicatedStorage.Remotes.FindPath.OnServerEvent:Connect(function(player)
    findPath(player)
end)

when i replace it with a npc it works but on the localplayer it finds the path but doesnt move the player

0
it is literally that the player is standing at one place? not sure about that but your code has one mistake, that is after humanoid:MoveTo(v.Position) you need humanoid.MoveToFinished:Wait() so the humanoid finsihes walking to the waypoint before walking to next one imKirda 4491 — 2y
0
yes i disable the controller so it stands in one place imKlevi 94 — 2y
0
when i add humanoid.MoveToFinished:Wait() the pathfinding stops imKlevi 94 — 2y

1 answer

Log in to vote
0
Answered by
Puppynniko 1059 Moderation Voter
2 years ago
Edited 2 years ago

you can try this to disable the players Movement note: this only disables all keyboard controls

local ContextActionService = game:GetService("ContextActionService")
local Name = "freezeMovement"

ContextActionService:BindAction(
    Name,
    function() return Enum.ContextActionResult.Sink end,
    false,
    unpack(Enum.PlayerActions:GetEnumItems())
)

type to unfreeze

ContextActionService:UnbindAction(Name)

and heres a gif on what it looks like with a small maze and your pathfinding streamable.com/q5s6u7 Full code

local ContextActionService = game:GetService("ContextActionService")
local PathfindingService = game:GetService("PathfindingService")
local Name = "freezeMovement"

ContextActionService:BindAction( --Stops player from moveing
    Name,
    function() return Enum.ContextActionResult.Sink end,
    false,
    unpack(Enum.PlayerActions:GetEnumItems())
)

local function findPath(player)
    local human = player.Character
    local humanoid = human.Humanoid
    local endDestination = workspace.Move
    local path = PathfindingService:CreatePath()
    path:ComputeAsync(human.HumanoidRootPart.Position, endDestination.Position)
    if path.Status == Enum.PathStatus.Success then
        local Nodes = path:GetWaypoints()
        for i,v in pairs(Nodes) do
            local node = Instance.new("Part", workspace)
            node.Size = Vector3.new(1,1,1)
            node.Position = v.Position
            node.Anchored = true
            node.CanCollide = false
            humanoid.WalkToPoint = v.Position
            humanoid:MoveTo(v.Position)
            humanoid.MoveToFinished:Wait()
        end
        ContextActionService:UnbindAction(Name)--Unlock player movement after the loop
    end
end

findPath(game:GetService("Players").LocalPlayer)
Ad

Answer this question