Need Help with the Slender AI Script

I have an issue with this script, I need to make that Slender Teleport AI work.

My AI should work like that:

  1. He teleports only behind you (and follows you) when he is not on your screen.

  2. He can teleports infront of you rarely at a long distance.

  3. When you look at him he cant move.

4.Once you collected more pages he becoming more aggressive (that means he teleports and following you more often).

there is my script i already made teleport time

local RunService = game:GetService("RunService")
local TS = game:GetService("TweenService")

local part = workspace:WaitForChild("Slender")
local player = game.Players.LocalPlayer
local character = player.Character
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")

local camera = workspace.CurrentCamera
local static = player.PlayerGui:WaitForChild("Static").ImageLabel

local pages = player:WaitForChild("Leaderstats").Pages

local distance = 100

local teleporttime = math.random(5,10)

local CanSeeSlender = false
coroutine.wrap(function()
while task.wait() do
	if pages.Value == 1 then
		distance = math.random(80,90)
		teleporttime = math.random(5,9.5)
	elseif pages.Value == 2 then
			distance = math.random(70,80)
			teleporttime = math.random(5,9.5)
	elseif pages.Value == 3 then
		distance = math.random(60,70)
		teleporttime = math.random(5,8.5)
	elseif pages.Value == 4 then
		distance = math.random(40,50)
		teleporttime = math.random(5,8)
	elseif pages.Value == 5 then
		distance = math.random(30,40)
		teleporttime = math.random(5,7.5)
	elseif pages.Value == 6 then
		distance = math.random(20,30)
		teleporttime = math.random(5,7)
	elseif pages.Value == 7 then
		distance = math.random(10,20)
		teleporttime = math.random(5,6)
	end
end
end)

RunService.RenderStepped:Connect(function()
	local a,see = workspace.CurrentCamera:WorldToScreenPoint(part.CFrame.Position)

	if see and part.Transparency == 0 then
		local ray = Ray.new(workspace.CurrentCamera.CFrame.Position,(part.CFrame.Position - workspace.CurrentCamera.CFrame.Position))
		local Part = workspace:FindPartOnRay(ray,game:GetService("Players").LocalPlayer.Character)
		if Part == part then
			CanSeeSlender = true
		else CanSeeSlender = false
		end
	else
		CanSeeSlender = false
	end
end)

coroutine.wrap(function()
	while task.wait(teleporttime) do
		if CanSeeSlender == false then
		part:PivotTo(character:GetPivot() - Vector3.new(0,0,distance))
		elseif CanSeeSlender == true then
		part.CFrame = part.CFrame
		end
	end
end)

I appreciate any help from everyone because its really hard to make and im trying to make AI for some months already