NPC walks through and into walls

I want the npc to walk around the walls even if it cant collide with them.

local TweenService = game:GetService("TweenService")

local Shrek = workspace:WaitForChild("Shrek")
local HumanoidRootPart = Shrek:WaitForChild("HumanoidRootPart")
local Humanoid = Shrek:WaitForChild("Humanoid")
local Locations = script.Parent
local RandomLocations = Locations:WaitForChild("RandomLocations")
local FoodBowl = Locations:WaitForChild("FoodBowl")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LowHunger = ReplicatedStorage:WaitForChild("RemoteEvents").LowHunger


local Path = PathFindingService:CreatePath()

local RandomLocation = RandomLocations:FindFirstChild(math.random(1,12))
Path:ComputeAsync(HumanoidRootPart.Position, RandomLocation.Position)
local Waypoints = Path:GetWaypoints()
Humanoid:MoveTo(RandomLocation.Position)
local MoveRandomly = true
local Stop = false

local Hunger
Humanoid.MoveToFinished:Connect(function(RandomLocation)
	LowHunger.Event:Connect(function(Player)
		Hunger = Player:WaitForChild("ShrekStats").Hunger
		if FoodBowl.FoodInBowl.Value == true then
			MoveRandomly = false
			Humanoid:MoveTo(FoodBowl.Position)
		end
	end)
	if MoveRandomly == true and Stop == false then
		wait(math.random(1,5))
		local NewLocation
		do repeat
				NewLocation = RandomLocations:FindFirstChild(math.random(1,12))
				wait()
			until
			NewLocation ~= RandomLocation
		end
		Path:ComputeAsync(HumanoidRootPart.Position, NewLocation.Position)
		local Waypoints = Path:GetWaypoints()
		for i, waypoint in pairs(Waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			Humanoid:MoveTo(waypoint.Position)
		end

	elseif MoveRandomly == false and Stop == false then
		Hunger.Value = 100
		FoodBowl.FoodInBowl.Value = false
		TweenService:Create(FoodBowl.Food, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Transparency = 1}):Play()
		MoveRandomly = true
		FoodBowl.Food.ParticleEmitter.Enabled = true
		wait(1)
		Hunger.Value = 100
		wait(1)
		Hunger.Value = 100
		wait(1)
		Hunger.Value = 100
		FoodBowl.Food.ParticleEmitter.Enabled = false
		local NewLocation
		do repeat
				NewLocation = RandomLocations:FindFirstChild(math.random(1,12))
				wait()
			until
			NewLocation ~= RandomLocation
		end
		Path:ComputeAsync(HumanoidRootPart.Position, NewLocation.Position)
		local Waypoints = Path:GetWaypoints()
		for i, waypoint in pairs(Waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			Humanoid.MoveToFinished:Wait(2)
			Humanoid:MoveTo(waypoint.Position)
		end
	end
end)

local ProximityPrompt = HumanoidRootPart:WaitForChild("ProximityPrompt")
local PetAnimationActivated = ReplicatedStorage:WaitForChild("RemoteEvents").PetAnimationActivated

ProximityPrompt.Triggered:Connect(function(Player)
	ProximityPrompt.Enabled = false
	Stop = true
	PetAnimationActivated:FireClient(Player)
	Humanoid:MoveTo(HumanoidRootPart.Position)
	local ShrekStats = Player:WaitForChild("ShrekStats")
	local HappinessStat = ShrekStats:WaitForChild("Happiness")
	if HappinessStat.Value <= 40 then
		HappinessStat.Value = HappinessStat.Value + 20
	else
		local ValueTo100 = 100-HappinessStat.Value
		HappinessStat.Value = HappinessStat.Value + ValueTo100
	end
end)

PetAnimationActivated.OnServerEvent:Connect(function()
	Stop = false
	Humanoid:MoveTo(RandomLocation.Position)
	ProximityPrompt.Enabled = true
end)


[quote="Pain, post:1, topic:2653966, full:true, username:TobiElReyofficial758"]
```local PathFindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")

local Shrek = workspace:WaitForChild("Shrek")
local HumanoidRootPart = Shrek:WaitForChild("HumanoidRootPart")
local Humanoid = Shrek:WaitForChild("Humanoid")
local Locations = script.Parent
local RandomLocations = Locations:WaitForChild("RandomLocations")
local FoodBowl = Locations:WaitForChild("FoodBowl")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LowHunger = ReplicatedStorage:WaitForChild("RemoteEvents").LowHunger


local Path = PathFindingService:CreatePath()

local RandomLocation = RandomLocations:FindFirstChild(math.random(1,12))
Path:ComputeAsync(HumanoidRootPart.Position, RandomLocation.Position)
local Waypoints = Path:GetWaypoints()
Humanoid:MoveTo(RandomLocation.Position)
local MoveRandomly = true
local Stop = false

local Hunger
Humanoid.MoveToFinished:Connect(function(RandomLocation)
	LowHunger.Event:Connect(function(Player)
		Hunger = Player:WaitForChild("ShrekStats").Hunger
		if FoodBowl.FoodInBowl.Value == true then
			MoveRandomly = false
			Humanoid:MoveTo(FoodBowl.Position)
		end
	end)
	if MoveRandomly == true and Stop == false then
		wait(math.random(1,5))
		local NewLocation
		do repeat
				NewLocation = RandomLocations:FindFirstChild(math.random(1,12))
				wait()
			until
			NewLocation ~= RandomLocation
		end
		Path:ComputeAsync(HumanoidRootPart.Position, NewLocation.Position)
		local Waypoints = Path:GetWaypoints()
		for i, waypoint in pairs(Waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			Humanoid:MoveTo(waypoint.Position)
		end

	elseif MoveRandomly == false and Stop == false then
		Hunger.Value = 100
		FoodBowl.FoodInBowl.Value = false
		TweenService:Create(FoodBowl.Food, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Transparency = 1}):Play()
		MoveRandomly = true
		FoodBowl.Food.ParticleEmitter.Enabled = true
		wait(1)
		Hunger.Value = 100
		wait(1)
		Hunger.Value = 100
		wait(1)
		Hunger.Value = 100
		FoodBowl.Food.ParticleEmitter.Enabled = false
		local NewLocation
		do repeat
				NewLocation = RandomLocations:FindFirstChild(math.random(1,12))
				wait()
			until
			NewLocation ~= RandomLocation
		end
		Path:ComputeAsync(HumanoidRootPart.Position, NewLocation.Position)
		local Waypoints = Path:GetWaypoints()
		for i, waypoint in pairs(Waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			Humanoid.MoveToFinished:Wait(2)
			Humanoid:MoveTo(waypoint.Position)
		end
	end
end)

local ProximityPrompt = HumanoidRootPart:WaitForChild("ProximityPrompt")
local PetAnimationActivated = ReplicatedStorage:WaitForChild("RemoteEvents").PetAnimationActivated

ProximityPrompt.Triggered:Connect(function(Player)
	ProximityPrompt.Enabled = false
	Stop = true
	PetAnimationActivated:FireClient(Player)
	Humanoid:MoveTo(HumanoidRootPart.Position)
	local ShrekStats = Player:WaitForChild("ShrekStats")
	local HappinessStat = ShrekStats:WaitForChild("Happiness")
	if HappinessStat.Value <= 40 then
		HappinessStat.Value = HappinessStat.Value + 20
	else
		local ValueTo100 = 100-HappinessStat.Value
		HappinessStat.Value = HappinessStat.Value + ValueTo100
	end
end)

PetAnimationActivated.OnServerEvent:Connect(function()
	Stop = false
	Humanoid:MoveTo(RandomLocation.Position)
	ProximityPrompt.Enabled = true
end)```

Try reading this:
Character Pathfinding | Documentation - Roblox Creator Hub
It describes how to set up paths, make modifiers, and ways to create paths around objects.

Already tried it but i dont know whats wrong with the script

I’m not too familiar with Pathfinding, but if the NPC isn’t moving correctly the pathfinding script may be fine, you just may not have the obstacles set up properly, so the path gets calculated through a wall where the NPC can’t move.
The Pathfinding engine needs to have the obstacles properly marked and set up so it’ll realize what is a possible path and what isn’t.

Check out the Working with Regions section of the previous link I posted which tells you how to add items that the path will calculate a route around.
Also configure your Navigation Visualization settings so you can see the areas that are allowed and not allowed to have paths inside them.

I figured out the solution is using this module:

Tutorial on how to use:

1 Like

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