My ai won’t follow me idk what i am doing wrong please help me
-- Services
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
-- Configuration
local SAFE_ZONE_TAG = "InSafeZone"
local ENEMY = workspace.Noob
local DETECTION_RADIUS = 50
-- Functions
local function isPlayerVisible(enemy, target)
local direction = (target.Position - enemy.HumanoidRootPart.Position).Unit
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {enemy}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(enemy.HumanoidRootPart.Position, direction * DETECTION_RADIUS, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
if hitPart and hitPart:IsDescendantOf(target.Parent) then
return true
end
end
return false
end
local function onEnterSafeZone(player)
if not player.Character then return end
player.Character:SetAttribute(SAFE_ZONE_TAG, true)
end
local function onExitSafeZone(player)
if not player.Character then return end
player.Character:SetAttribute(SAFE_ZONE_TAG, nil)
end
local function moveToTarget(enemy, target)
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentJumpHeight = 7,
AgentMaxSlope = 45
})
path:ComputeAsync(enemy.HumanoidRootPart.Position, target.Position)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
enemy:MoveTo(waypoint.Position)
enemy.Humanoid.MoveToFinished:Wait()
end
end
end
local function detectAndChase(enemy)
while task.wait(1) do
local partsInRadius = workspace:GetPartBoundsInRadius(enemy.HumanoidRootPart.Position, DETECTION_RADIUS)
for _, part in ipairs(partsInRadius) do
local player = Players:GetPlayerFromCharacter(part.Parent)
if player and not player.Character:GetAttribute(SAFE_ZONE_TAG) then
if isPlayerVisible(enemy, part) then
moveToTarget(enemy, part)
end
end
end
end
end
local safeZone = workspace.Safezone
safeZone.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
onEnterSafeZone(player)
end
end)
safeZone.TouchEnded:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
onExitSafeZone(player)
end
end)
task.spawn(function()
detectAndChase(ENEMY)
end)