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.
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)
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).