So recently I've made my first attempt at making a little pathfinding ai using roblox's pathfindingservice. It's still a little bit buggy sometimes. And recently I found out that sometimes it first walks to a weird point on the map before actually walking to the destination.
Here's the script, it's quite long.
ServerSided script in ServerScriptService:
--by blacksmiley0 local Rex = game.Workspace:WaitForChild("Rex") local Humanoid = Rex.Humanoid local Delay = 1 local ObjectiveReached = false local y1 = 0 local y2 = 0 local Time = 0 local Time2 = 0 local distance2 = 0 local ShowPath = false --Show Path local times = 0 local NewDistance = 1 function WalkTo(Start, Objective) local path = game:GetService("PathfindingService"):ComputeRawPathAsync(Start, Objective, 500) --Create a Path local points = path:GetPointCoordinates() --Create Table with Coordinates if Enum.PathStatus.Success then --Check if we could compute a Path if ShowPath then --Create blocks on the points to visualize path, set this to true if you want to see the path the npc will take. for p = 1, #points do local part = Instance.new("Part") part.FormFactor = Enum.FormFactor.Symmetric part.CanCollide = false part.Size = Vector3.new(1,1,1) part.Position = points[p] part.Anchored = true part.Parent = game.Workspace.Points end end for p = 1, #points do local path = game:GetService("PathfindingService"):ComputeRawPathAsync(Start, Objective, 500) --Create a Path local points = path:GetPointCoordinates() --Create Table with Coordinates if ShowPath then --Create blocks on the points to visualize path, set this to true if you want to see the path the npc will take. for p = 1, #points do local part = Instance.new("Part") part.FormFactor = Enum.FormFactor.Symmetric part.CanCollide = false part.Size = Vector3.new(1,1,1) part.Position = points[p] part.Anchored = true part.BrickColor = BrickColor.new(0, 0, 0) part.Parent = game.Workspace.Points end end if points[p] ~= nil then if (Rex.Torso.Position - points[p]).Magnitude > 9 then local path2 = game:GetService("PathfindingService"):ComputeRawPathAsync(Rex.Torso.Position, points[p], 100) --Create a Path local points2 = path2:GetPointCoordinates() --Create Table with Coordinates for i = 1, #points2 do if ShowPath then --Create blocks on the points to visualize path, set this to true if you want to see the path the npc will take. for i = 1, #points2 do local part = Instance.new("Part") part.FormFactor = Enum.FormFactor.Symmetric part.CanCollide = false part.Size = Vector3.new(1,1,1) part.Position = points2[i] part.Anchored = true part.BrickColor = BrickColor.new(1, 0, 0) part.Parent = game.Workspace.Points2 end end if points2[i] ~= nil then Humanoid:MoveTo(points2[i]) --Move to the desired position if i ~= 1 then --Checking if he needs to jump. Make sure you do not try to index p - 1 when p = 1 because then you get 0 local y1 = points2[i - 1] local y2 = points2[i] if y1.Y < y2.Y then --Check if Y coordinate has changed to see if we need to jump wait(0.3) Rex.Humanoid.Jump = true end end repeat wait() NewDistance = (Rex.Torso.Position - points2[i]).Magnitude until NewDistance < 4 end end end Humanoid:MoveTo(points[p]) --Move to the desired position if p ~= 1 then --Checking if he needs to jump. Make sure you do not try to index p - 1 when p = 1 because then you get 0 local y1 = points[p - 1] local y2 = points[p] if y1.Y < y2.Y then --Check if Y coordinate has changed to see if we need to jump wait(0.3) Rex.Humanoid.Jump = true end end repeat wait() local distance = (game.Workspace.Rex.Torso.Position - points[p]).Magnitude if p ~= #points then distance2 = (game.Workspace.Rex.Torso.Position - points[p + 1]).Magnitude else distance2 = 7 end Time = Time + 1 if Time >= 25 then --If Rex takes to much time to get to the next position, he will jump. Rex.Humanoid.Jump = true end until distance < 4 or distance2 < 4 --Check if we are close enough to the point end Time = 0 end end end script.Activate.Changed:connect(function(Value) --This is so we can activate the pathfinding from another script by simply changing the values inside the script. if Value then script.Activate.Value = false WalkTo(script.Start.Value, script.Finish.Value) Rex.Torso.Anchored = true script.Finished.Value = true end end)
if you see anything that I should change, please tell me.