Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

3 comments

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

0

yes i disable the controller so it stands in one place

0

when i add humanoid.MoveToFinished:Wait() the pathfinding stops

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.