I am currently making a SCP game and i have pathfinding and stuff but with the pathfinding the waypoints dont catch up with the player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Help for my pathfinding script
  2. What is the issue? Include screenshots / videos if possible!
    Stuttering/slow pathfinding
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i have for an embarrasing amount of time
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

i forgot to show the script its REALLY messy and insuficent but here it is :

local pathfinding = game:GetService(“PathfindingService”)
local char = script.Parent
local hum = char:WaitForChild(“Humanoid”)
local hrp = char:WaitForChild(“HumanoidRootPart”)

Path1 = pathfinding:CreatePath()
Path2 = pathfinding:CreatePath() – Wandering

local newpart = Instance.new(“Part”)
newpart.Parent = workspace
newpart.Transparency = 1
newpart.CanCollide = false
newpart.Anchored = true
newpart.Position = script.Parent.Torso.Position * Vector3.new(math.random(1,10),0,math.random(1,10))
newpart.Name = script.Parent.Name

local function movetopath(location)
if Path1.Status == Enum.PathStatus.Success then
hum:MoveTo(location)
end
end

local function randommove(location)
if Path2.Status == Enum.PathStatus.Success then
hum:MoveTo(location)
end
end

while true do

game:GetService("RunService").Stepped:Wait()
print("A")
	for i,Child in pairs(game.Workspace:GetChildren()) do -- Checks for Players in workspace
		if Child.Name:sub(0,3) ~= "SCP" and Child.Name:sub(0,3) ~= "Inf" and Child.Name:sub(0,3) ~= "Rig" and Child:FindFirstChild("HumanoidRootPart") then
			local plr = game.Players:GetPlayerFromCharacter(Child)
			if (Child.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude <= 150 then
				Path1:ComputeAsync(script.Parent.HumanoidRootPart.Position,Child.HumanoidRootPart.Position)
				if Path1.Status == Enum.PathStatus.Success then
					local waypoints = Path1:GetWaypoints()
					script.Parent.Humanoid.Moving.Value = true
					if script.Parent.Humanoid.Sitting.Value == true then
						wait(6)
					end
				for i,v in pairs(waypoints) do
				script.Parent.Humanoid:MoveTo(v.Position)
				script.Parent.Humanoid.MoveToFinished:Wait()
			end
		end
			else
			script.Parent.Humanoid.Moving.Value = false
		script.Parent.Humanoid.Sitting.Value = true
		end
	end
end

end

2 Likes

Going to bed right now appreciate any replys when i wake up

1 Like

whenever you update the path you have to cancel the logic for the previous path, It looks like your npc is following out of date waypoints.

1 Like

how do I cancel it? im kinda slow lol

I have fixed my problem by myself I appreciate everyone who helped me and if anyone want’s a pathfinding script : insert a script inside the rig and paste this inside of it! :

local pathfinding = game:GetService(“PathfindingService”)
local char = script.Parent
local hum = char:WaitForChild(“Humanoid”)
local hrp = char:WaitForChild(“HumanoidRootPart”)

Path1 = pathfinding:CreatePath()
Path2 = pathfinding:CreatePath() – Wandering Remove

for i,BasePart in pairs(script.Parent:GetChildren()) do
if BasePart:IsA("BasePart") or BasePart:IsA("MeshPart") then
	BasePart:SetNetworkOwner(nil)

end
end

local function Stuck(v)
	script.Parent.Humanoid:MoveTo(v.Position)
end

while true do

wait(0.1)

for i,Child in pairs(game.Workspace:GetChildren()) do -- Checks for Players in workspace
	if Child:FindFirstChild("HumanoidRootPart") then
		local plr = game.Players:GetPlayerFromCharacter(Child)
		if (Child.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).Magnitude <= 50 then -- Change 50 to whatever distance you want your NPC to lock onto you from higher means further away less means less further
			Path1:ComputeAsync(script.Parent.HumanoidRootPart.Position,Child.HumanoidRootPart.Position)
			if Path1.Status == Enum.PathStatus.Success then
				local waypoints = Path1:GetWaypoints()
				if tonumber(script.Parent.WalkSpeed.Value) == 2 or tonumber(script.Parent.WalkSpeed.Value) > 2 then
					script.Parent.Humanoid:MoveTo(plr.Character.HumanoidRootPart.Position)
					print('No Waypoitns')
					else
					for i,v in pairs(waypoints) do
						Stuck(v)
						script.Parent.Humanoid.MoveToFinished:Wait()
					end
					if tonumber(script.Parent.WalkSpeed.Value) == 2 or tonumber(script.Parent.WalkSpeed.Value) > 2 then
						script.Parent.Humanoid:MoveTo(plr.Character.HumanoidRootPart.Position)
						print('No waypoints')
				    end
				end
			end
		end
	end
end

end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.