Problem with ai

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)

it should follow me then when i enter safe zone it should not come for me

chat gpt gave me this

– Services
local PathfindingService = game:GetService(“PathfindingService”)
local Players = game:GetService(“Players”)

– Configuration
local SAFE_ZONE_TAG = “InSafeZone”
local ENEMY = workspace:WaitForChild(“Noob”) – Ensures the enemy exists
local DETECTION_RADIUS = 50

– Functions
local function isPlayerVisible(enemy, target)
if not enemy or not target or not enemy:FindFirstChild(“HumanoidRootPart”) then
return false
end

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 player.Character then
player.Character:SetAttribute(SAFE_ZONE_TAG, true)
end
end

local function onExitSafeZone(player)
if player.Character then
player.Character:SetAttribute(SAFE_ZONE_TAG, nil)
end
end

local function moveToTarget(enemy, target)
if not enemy or not target or not enemy:FindFirstChild(“Humanoid”) or not enemy:FindFirstChild(“HumanoidRootPart”) then
return
end

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.Humanoid:MoveTo(waypoint.Position)
enemy.Humanoid.MoveToFinished:Wait()
end
end
end

local function detectAndChase(enemy)
while task.wait(1) do
if not enemy or not enemy:FindFirstChild(“HumanoidRootPart”) or not enemy:FindFirstChild(“Humanoid”) then
return
end

  local partsInRadius = workspace:GetPartBoundsInRadius(enemy.HumanoidRootPart.Position, DETECTION_RADIUS)
  for _, part in ipairs(partsInRadius) do
  	local player = Players:GetPlayerFromCharacter(part.Parent)
  	if player and player.Character and not player.Character:GetAttribute(SAFE_ZONE_TAG) then
  		if isPlayerVisible(enemy, part) then
  			moveToTarget(enemy, part)
  			break -- Only chase one target at a time
  		end
  	end
  end

end
end

– Safe Zone Logic
local safeZone = workspace:FindFirstChild(“Safezone”)
if safeZone then
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)
end

– Start the enemy detection and chasing
task.spawn(function()
detectAndChase(ENEMY)
end)

sorry it got chopped off a bit try pasting it now

:skull: (charrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr)

You’re having an issue here. Touched and TouchedEnded are similar signal functions.

Touched is when something touches it

TouchedEnded is when something leaves the part after it touched it.

You should probably add a cooldown so that it doesn’t take away your safezone tag.

ChatGPT won’t really help much.

the ai does not work not the safe zone