Hey devs Im having trouble with the pathfinding script that follows the closest player I want it that when it reaches a certain distance it kills the player but the NPC doesnt even move that’s the problem and My Code doesnt have any errors
I saw that you added a “PathFinding” variable in your script but I dont think I see you using it in your function. Do you want any help on setting this Pathfinding system?
hi sorry for the late reply yes It would really help
You could consider watching a tutorial like this one: Advanced Roblox Scripting Tutorial #21 - Pathfinding (Beginner to Pro 2019) - YouTube. But since this is very long, let me just summarize the important parts for you.
You could create a variable with this value inside it, “PathFindingService:CreatePath()”, then you would create a path with the ComputeAsync function. First you put the NPC Position. After that, the target position. To get the waypoints, you use the same variable that you created and use the GetWaypoints function. You would have to use it in a loop. Create a for function and use the waypoints that you created with pairs(). Inside that loop, make the Humanoid move to the waypoint position. You get the position the same way you get the position in a BasePart. After this, you need to add a MoveToFinished function to that humanoid with the Wait function in the following. You put all of this in a loop, except the first variable where you have to create the path.
I apologize if you don’t understand, this is my first time explaining a short tutorial.
Or you could copy this instead:
Without PathFindingService:
local Player = game:GetService("Players")
local RunService = game:GetService("RunService")
local dummy = script.Parent
local humanoid = dummy:WaitForChild("Humanoid")
local rootpart = dummy:WaitForChild("HumanoidRootPart")
local function pathfinding()
local target
local players = Player:GetPlayers()
for _,player in pairs(players) do
local char = player.Character or player.CharacterAdded:Wait()
if not char then continue end
local playerRootPart = char:FindFirstChild("HumanoidRootPart")
if not playerRootPart then continue end
if target and (rootpart.Position - target.Position).Magnitude > (rootpart.Position - playerRootPart.Position).Magnitude then
target = playerRootPart
continue
end
target = playerRootPart
end
if not target then return end
humanoid:MoveTo(target.Position)
end
RunService.Stepped:Connect(pathfinding)
With PathFindingService:
local Player = game:GetService("Players")
local RunService = game:GetService("RunService")
local PathFindingService = game:GetService("PathfindingService")
local dummy = script.Parent
local humanoid = dummy:WaitForChild("Humanoid")
local rootpart = dummy:WaitForChild("HumanoidRootPart")
local path = PathFindingService:CreatePath()
local debounce = false
local function waypoint(startPos, endPos)
if debounce == true then return end
debounce = true
path:ComputeAsync(startPos, endPos)
local wayPoints = path:GetWaypoints()
for i,v in pairs(wayPoints) do
humanoid:MoveTo(v.Position)
--humanoid.MoveToFinished:Wait()
end
debounce = false
end
local function pathfinding()
local target
local players = Player:GetPlayers()
for _,player in pairs(players) do
local char = player.Character or player.CharacterAdded:Wait()
if not char then continue end
local playerRootPart = char:FindFirstChild("HumanoidRootPart")
if not playerRootPart then continue end
if target and (rootpart.Position - target.Position).Magnitude > (rootpart.Position - playerRootPart.Position).Magnitude then
target = playerRootPart
continue
end
target = playerRootPart
end
if not target then return end
waypoint(rootpart.Position, target.Position)
end
RunService.Stepped:Connect(pathfinding)
You could programm it to make it jump when there’s an obstacle.
These scripts are intentionally designed to be put inside the NPC Model.
You use “continue” if you want to skip to the next loop.
yes
Thank you very much I will implement that rn and see the outcome thank you for your time.
Hi I tested the script it still doesnt work
I aplologize that your script doesn’t work. I am not fully experienced with PathFindingService. But it is ironic that the script worked in my NPC.
LOL yuh Idk myself maybe theirs a problem with the npc since it is a free model but umma solve that later rn now I working on the back end since im making a horror game.