So my NPC goes through walls and is dumb? how can I make this into pathfinding? I have no idea. I’ve read the wiki on the pathfinder but no clue.
local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local selectedPlayer
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 10000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
selectedPlayer = temp
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(math.random(1,5))
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
while true do
wait(math.random(1,5))
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target.Position)
end
end
Take a look at this comment. It does what you asked.
Here is the code
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath{
AgentRadius = 2.0,
AgentHeight = 5.0,
AgentCanJump = false
}
local waypoints
local arrival = 0
local oldtime = tick()
local character = script.Parent --- ur character
local Cx, Cz = character.PrimaryPart.Position.X, character.PrimaryPart.Position.Z -- Your Center Point
local radius = 50 -- Your Radius
local stayTime = 4 -- How Long you want the character to wait at destination (Seconds)
local PathParts = {}
function drawCircle() -- DELETE IF YOU WANT THE CIRCLE TO NOT SHOW
for i = 0, 360, 5 do
local angle = (i * math.pi) / 180
local x, y, z = math.cos(angle ) * radius , 0, math.sin(angle) * radius
local part = Instance.new("Part",workspace)
part.Position = Vector3.new(x,y,z)
part.Anchored = true
part.Size = Vector3.new(1,1,1)
part.Material = Enum.Material.Neon
part.CanCollide = false
end
end
function drawPath(waypoint) -- DELETE IF YOU WANT THE PATH TO NOT SHOW
local part = Instance.new("Part",workspace)
part.Position = waypoint.Position
part.Anchored = true
part.Size = Vector3.new(1,1,1)
part.Material = Enum.Material.Neon
part.CanCollide = false
part.Color = Color3.fromRGB(255, 0, 0)
table.insert(PathParts, part)
end
local function followPath(character, destination)
local humanoid = character.Humanoid
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
waypoints = path:GetWaypoints()
local distance = (character.PrimaryPart.Position - waypoints[#waypoints].Position).Magnitude
arrival = (distance/humanoid.WalkSpeed) + stayTime
for i,waypoint in pairs(waypoints) do
drawPath(waypoint) -- DELETE IF YOU WANT THE PATH TO NOT SHOW
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:wait()
end
end
end
drawCircle() -- DELETE IF YOU WANT THE CIRCLE TO NOT SHOW
while wait() do
local angle = math.random(0,2*math.pi)-- Random angle in radians
local x, y, z = math.cos(angle ) * radius , character.PrimaryPart.Position.Y, math.sin(angle) * radius -- end poistion
local humanoid = character.Humanoid --- ur humanoid
if tick() - oldtime > arrival then
for i,v in pairs(PathParts) do
v:Destroy()
end
followPath(script.Parent, Vector3.new(x,y,z))
isReturned = false
oldtime = tick()
end
end
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath{
AgentRadius = 2.0,
AgentHeight = 5.0,
AgentCanJump = false
}
local waypoints
local arrival = 0
local oldtime = tick()
local character = script.Parent --- ur character
local Cx, Cz = character.PrimaryPart.Position.X, character.PrimaryPart.Position.Z -- Your Center Point
local radius = 50 -- Your Radius
local stayTime = 4 -- How Long you want the character to wait at destination (Seconds)
local PathParts = {}
function drawPath(waypoint) -- DELETE IF YOU WANT THE PATH TO NOT SHOW
local part = Instance.new("Part",workspace)
part.Position = waypoint.Position
part.Anchored = true
part.Size = Vector3.new(1,1,1)
part.Material = Enum.Material.Neon
part.CanCollide = false
part.Color = Color3.fromRGB(255, 0, 0)
table.insert(PathParts, part)
end
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 10000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
selectedPlayer = temp
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
local function followPath(character, destination)
local humanoid = character.Humanoid
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
waypoints = path:GetWaypoints()
local distance = (character.PrimaryPart.Position - waypoints[#waypoints].Position).Magnitude
arrival = (distance/humanoid.WalkSpeed) + stayTime
for i,waypoint in pairs(waypoints) do
drawPath(waypoint) -- DELETE IF YOU WANT THE PATH TO NOT SHOW
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:wait()
end
end
end
while wait() do
local angle = math.random(0,2*math.pi)-- Random angle in radians
local humanoid = character.Humanoid --- ur humanoid
if tick() - oldtime > arrival then
for i,v in pairs(PathParts) do
v:Destroy()
end
followPath(character, findNearestTorso())
isReturned = false
oldtime = tick()
end
end