So I have been making a horror game and I am currently making the monster’s AI. I have a small lack of knowledge with the pathfinding service but I found a tutorial on how to use it. The problem was the monster would always follow the player even if it was behind a wall. So I added a raycast thingy and now it kind of works. But now I am kind of stuck because the monster always casts a raycast and if the player is within the monster’s range but the player is behind the a wall, the monster gets stuck. Any help is appreciated! Here is my code (Check the walkTo function that is where im stuck):
local pathfinding = game:GetService("PathfindingService")
local plrs = game:GetService("Players")
local runservice = game:GetService("RunService")
local guy = script.Parent
local hum = guy:WaitForChild("Humanoid")
guy.PrimaryPart:SetNetworkOwner(nil)
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local WalkSpeed = guy:GetAttribute("WalkSpeed")
local SprintSpeed = guy:GetAttribute("SprintSpeed")
local Damage = guy:GetAttribute("Damage")
local MaxDistance = guy:GetAttribute("MaxDistance")
local a = script.Parent:WaitForChild("RaycastStart")
local exludeList = {}
local spawnPoint = guy:WaitForChild("HumanoidRootPart").Position
for i, v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
if v.Transparency > 0 and v.Transparency < 1 then
table.insert(exludeList, v)
end
end
end
local function getPath(destination)
local path = pathfinding:CreatePath({
AgentHeight = 6;
AgentRadious = 3;
AgentCanJump = false;
Costs = {
Water = 100;
DangerZone = math.huge
}
})
path:ComputeAsync(guy.HumanoidRootPart.Position, destination.Position)
return path
end
local function findTarget()
local nearestTarget
local maxDistance = MaxDistance
for index, player in pairs(plrs:GetPlayers()) do
if player.Character then
local target = player.Character
local distance = (guy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < MaxDistance then
nearestTarget = target
end
end
end
return nearestTarget
end
local function Capture(target)
local distance = (guy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 3 then
hum:MoveTo(target.HumanoidRootPart.Position)
else
print("caught you >:)")
end
end
local function resetAndGoToSpawn()
guy:MoveTo(spawnPoint)
end
local function raycastToPlayer(target)
local target = target:WaitForChild("Target")
local dir = target.Position - a.Position
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = exludeList
local raycastResult = workspace:Raycast(a.Position, dir, params)
if raycastResult and raycastResult.Instance then
if raycastResult.Instance == target then
return true
else
return false
end
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target then
local shouldCapture = raycastToPlayer(target)
if shouldCapture == true then
Capture(target)
guy.Humanoid.WalkSpeed = SprintSpeed
break
end
else
guy.Humanoid.WalkSpeed = WalkSpeed
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
end
else
hum:MoveTo(destination.Position - (guy.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function patrol()
local waypoints = workspace.Waypoints:GetChildren()
local randomWaypoint = math.random(1, #waypoints)
walkTo(waypoints[randomWaypoint])
end
while task.wait(0.01) do
patrol()
if guy.HumanoidRootPart.Position.Y < -200 then
resetAndGoToSpawn()
end
end