Hi I am trying to make a player chasing AI. It works fine but the player can easily just go around a wall and it becomes useless. I have tried putting in a basic pathfinding thing in but that did not work and it becomes super glitchy. Can anyone explain how I can fix this?
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local humanoid = script.Parent
local root = humanoid.Parent.PrimaryPart
local targetDistance = script:GetAttribute("TargetDistance")
local stopDistance = script:GetAttribute("StopDistance")
local damage = script:GetAttribute("Damage")
local attackDistance = script:GetAttribute("AttackDistance")
local attackWait = script:GetAttribute("AttackWait")
local lastAttack = tick()
function findNearestPlayer()
local playerList = Players:GetPlayers()
local nearestPlayer = nil
local distance = nil
local direction = nil
for _, player in pairs(playerList) do
local character = player.Character
if character then
local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
if not nearestPlayer then
nearestPlayer = player
distance = distanceVector.Magnitude
direction = distanceVector.Unit
elseif distanceVector.Magnitude < distance then
nearestPlayer = player
distance = distanceVector.Magnitude
direction = distanceVector.Unit
end
end
end
return nearestPlayer, distance, direction
end
local soundSpamStop = false
wait(5)
RunService.Heartbeat:Connect(function()
local nearestPlayer, distance, direction = findNearestPlayer()
if nearestPlayer then
if distance <= targetDistance and distance >= stopDistance then
if nearestPlayer.Character.Humanoid.Health > 0 then
humanoid:Move(direction)
end
end
end
end)