Custom NPC wont move as intended


#1

So all my npc does is rotate and howl slowly and I’m confused on why its not and the NPC I looked off of for the code does.
On a timeframe and I know the pathfinding method is deprecated not worried at all about that please just post the fix with what was wrong so I know for next time.

Moving Code

local Hum = script.Parent.Humanoid or script.Parent:WaitForChild('Humanoid')
local Path = game:GetService("PathfindingService")
local config = script.Parent.Configurations
local targetBots = false
local targetV = script.Parent:WaitForChild("Target")

function FindEnemies()
	local Hums = {}
	if targetBots then
		local function Find(x)
			for _,obj in pairs(x:GetChildren()) do
				if obj:IsA("Model") then
					if obj:FindFirstChild'Humanoid' and obj:FindFirstChild'HumanoidRootPart' and obj.Humanoid.Health > 0 and not obj:FindFirstChild'NPC' and not obj:FindFirstChild'ForceField' then
						table.insert(Hums,obj.Humanoid)
					end
				end
				Find(obj)
			end
		end
		Find(workspace)
	else
		for _,v in pairs(game.Players:GetPlayers()) do
			if v.Character and v.Character:FindFirstChild'HumanoidRootPart' and v.Character:FindFirstChild'Humanoid' and v.Character.Humanoid.Health > 0 and not v:FindFirstChild'ForceField' and not v.Character:FindFirstChild'AFK'  then
				table.insert(Hums, v.Character.Humanoid)
			end
		end
	end 
	local closest,path,dist
	for i,hum in pairs(Hums) do
		local MyPath = Path:ComputeSmoothPathAsync(Hum.Parent.HumanoidRootPart.Position, hum.Parent.HumanoidRootPart.Position, 20)
		if MyPath.Status ~= Enum.PathStatus.FailFinishNotEmpty then
			local myDist = 0
			local prev = Hum.Parent.HumanoidRootPart.Position
			for _,point in pairs(MyPath:GetPointCoordinates()) do
				myDist = myDist + (point-prev).magnitude
				prev = point
			end
			if not dist or myDist < dist then
				closest = Hum
				path = MyPath
				dist = myDist
			end
		end
	end
	return closest,path
end
	
function goToPos(loc)
	Hum:MoveTo(loc)
	
	local distance = (loc-Hum.Parent.HumanoidRootPart.Position).magnitude
	local start = tick()
	while distance > 4 do
		if tick()-start > distance/Hum.WalkSpeed then
			break
		end
		distance = (loc-Hum.Parent.HumanoidRootPart.Position).magnitude
		wait()
	end
end	

while wait() do
	local target,path = FindEnemies()
	local didBreak = false
	local targetStart
	if target and Hum.Parent.HumanoidRootPart then
		targetV.Value = target
		targetStart = target.Parent.HumanoidRootPart.Position
		roaming = false
		local prev = Hum.Parent.HumanoidRootPart.Position
		local points = path:GetPointCoordinates()
		local s = #points > 1 and 2 or 1
		for i = s,#points do
			local point = points[i]
			if didBreak then
				break
			end
			if target and target.Parent.HumanoidRootPart and target.Health > 0 then
				if (target.Parent.HumanoidRootPart.Position-targetStart).magnitude < 1.5 then
					local pos = prev:lerp(point,.5)
					local moveDir = ((pos - Hum.Parent.HumanoidRootPart.Position).unit * 2)
					goToPos(prev:lerp(point,.5))
					prev = point
				end
			else
				didBreak = true
				break
			end
		end
	else
		targetV.Value = nil
	end
	if not didBreak and targetStart then
		goToPos(targetStart)
	end
end
	```
**Animate Code**
```Lua
local Hum = script.Parent.Humanoid or script.Parent:WaitForChild(&quot;Humanoid&quot;)

local Config = script.Parent.Configurations

local Pose = &quot;Standing&quot;

---Functions

function Walking(speed)

if speed &gt; .5 then

local scale = 10

local Walk = Hum:LoadAnimation(Config.Walking)

Walk:Play()

Pose = &quot;Running&quot;

else

Pose = &quot;idle&quot;

if Pose == &quot;idle&quot; then

local idle = Hum:LoadAnimation(Config.Idle)

idle:Play()

end

end

end

function Dead()

Pose = &quot;Died&quot;

print'dead'

local noise = Config.Screech

noise.Volume = 10

noise:Play()

end

---Connections

Hum.Running:Connect(Walking)

Hum.Died:Connect(Dead)

Custom NPC Pic


#2

Could you post a video showing the current behavior?


#3

Uh, I can in the morning, it’s currently 2:20 am for me.


#4

So the Quotes in the code block are displaying as the site value don’t know if that’s a site bug or not


#5

Probably because your code block isn’t formatted correctly.

More information is required overall for this thread. A video of how the NPC is currently behaving, potential console errors, object hierarchy, so on so forth would be helpful to assist with the problem.


#6

No console errors already know that, I think I have it down to the npc is not animating right. Probably the animate script


#7

Animations shouldn’t interfere with pathfinding unless you’re using CFrame to animate. We do really need the video of the NPC’s behavior to understand more.


#8

Its as if the NPC is confused on where it wants to go, also the walking anim tends to not play right from the get go.


#9

Hmm it seems like an error in pathfinding.

Have you checked with visual nodes? (Placing parts along the waypoints)

To me it seems like it’s trying to go to copy and you at the same time. It may have to do with using deprecated methods but I’m not entirely sure.


#10

Let me try adding an ignore


#11

Ok the clone of me is being ignored by adding a value tricking the hollow into thinking its a friend of it and it still acts similar. It’s a path finding error


#12

So how would I fix this to act normal? To where it just follows the player then I can script the attack.


#13

Try visualizing the nodes as suggested previously. If it’s the pathfinding that is off then it will show.


#14

https://gyazo.com/862c2f7b2db2f3b43da53898557b7848 welp what is going on here


#15

Hmm that doesn’t seem normal…

Try updating the path finding methods to non-deprecated methods.

If the problem still persists then at the very least you’ve updated your code.


#16

If the NPC is that close you might as well use :MoveTo() to simplify things until the NPC gets out of range.


#17

I think he wants to keep the pathfinding aspect of it so that way it can navigate around obstacles which I’m almost certain will be a thing in the finished product.


#18

I don’t want my npc’s running into walls.


#19

I want the fear of the npc running around as the players think the npc will run into the wall but actually go around and get them guud.


#20

Have you tried updating your methods yet?

If so, how did it turn out?