Hello, can someone help me with this? the AI can detect the player even though player is not touching the raycast… here’s the code
I’m kinda bad at scripting so please don’t judge
local npc = script.Parent
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {npc.Head}
local pathfindingService = game:GetService("PathfindingService")
local humanoid = npc.Humanoid
local lastSeenTime = 0
local detectionDistance = 50
while true do
for _, player in ipairs(game.Players:GetPlayers()) do
if player.Character then
local origin = npc.Head.Position
local direction = npc.Head.CFrame.LookVector * detectionDistance
local playerModel = player.Character
local playerParts = {}
for _, part in ipairs(playerModel:GetDescendants()) do
if part:IsA("BasePart") then
table.insert(playerParts, part)
end
end
-- add the player model to the ignore list
table.insert(playerParts, playerModel)
local result = workspace:FindPartOnRayWithIgnoreList(Ray.new(origin, direction), playerParts)
if result and (result.Position - npc.Head.Position).Magnitude <= detectionDistance then
lastSeenTime = tick()
local path = pathfindingService:CreatePath()
path:ComputeAsync(npc.Head.Position, playerModel.HumanoidRootPart.Position)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
end
end
if tick() - lastSeenTime > 2 then
humanoid:MoveTo(npc.Head.Position)
end
wait()
end