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

How do use pathfinding on local player?

I am trying to make it so if a certain player touches another player, the touched player will run a pathfinding script and walk there. what I've done is I will fire all clients and from a local script, it will check if the given name matches localplayer's name and if so, it will make the player go to a part inside of workspace. Everything works except the pathfinding service, which has an error:

Unable to cast to Dictionary

In the following picture you can see that, on line 1, it returns the correct name.

Photo

I did borrow this code from another post and modify it.

Script:

local plr = workspace[p.Parent.Name]
		local player = game.Players:GetPlayerFromCharacter(plr)
		local tagged = p.Parent.Name
        print("tagger tagged "..tagged) -- this works as intentioned
		game.ReplicatedStorage.RE.was_tagged:FireAllClients(p,tagged)

LocalScript:

function wasTagged(p,tagged)
    if tagged == game.Players.LocalPlayer.Name then
    print("Player reconized as the player who has gotten tagged ("..tagged..")") -- -- this works as intentioned
		local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
		local Controls = PlayerModule:GetControls()
		Controls:Disable()
    local Pathfinding = game:GetService("PathfindingService")
local nodes = {
    game.Workspace[tagged].Torso,
    game.Workspace.CurrentMap.Map.taggedPoint,
}
local path = Pathfinding:CreatePath(nodes[1], nodes[2], 500) -- this is what is mentioned in the error
if path.Status == Enum.PathStatus.Success then
    local humanoid = game.Workspace[tagged].Humanoid
    local points = path:GetPointCoordinates()
    for _, point in ipairs (points) do
        humanoid:MoveTo(point)
        repeat
	local distance = (point - humanoid.Torso.Position).magnitude
	wait()
until distance <= 3

    end
else
    warn("Path creation unsuccessful")
end
end
end
game.ReplicatedStorage.RE.was_tagged.OnClientEvent:Connect(wasTagged)

5 comments

0

You're not using CreatePath() correctly and :GetPointCoordinates() is deprecated.

0

what do I use...? roblox wiki makes so sense

1

It's been replaced with :GetWaypoints(). The main problem is line 12 which needs an int value. You're indexing objects there. I don't want to ghost anyone on this since I'm not on my computer. This deserves a good explanation.

0

Just wondering if anyone has an alternative to the roblox wiki because as Superman said, it makes no sense sometimes, does anyone have a better source of information?

0

the answer: no. YouTube is a pile of steaming buffalo crap and the Roblox Wiki is your one reliable source for something like this.

1 answer

Roblox's wiki for pathfinding is really bad for some reason. After going through it several times recently, I happened across this essential article: https://developer.roblox.com/articles/Pathfinding

It is the only place that explains the arguments you need for CreatePath -- it takes in only one optional argument: a dictionary with keys AgentHeight and AgentRadius. The idea is that you then take the returned path and call ComputeAsync (only documented in that article and here: https://developer.roblox.com/api-reference/function/Path/ComputeAsync -- not even referenced in the Path class!) with your start/destination location.

Note that there doesn't seem to be a "maxDistance" parameter anywhere; if that's important to you, you might want to use the deprecated function ComputeRawPathAsync, though I suspect the Path will work just fine for you.

The article also gives examples on working with the Path.Blocked event, which might be useful if your map is dynamic. (Do note that pathfinding ignores all models with a humanoid in them, assuming that they're a character that can/will move at any time.)

By the way, if the other clients don't need to know about this "tagged" player, you should use FireClient(player) instead of FireAllClients(player.Name) (which is effectively what you're doing) - saves a little bandwidth and simplifies your scripts (since then clients don't have to check whether they're the tagged client or not).

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