How do i make an NPC teleport behind the player if the player is not looking at it

Here you go.

local RunService = game:GetService "RunService"
local Players = game:GetService "Players"

local Camera = workspace.CurrentCamera
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Stan = workspace.Stan

local Debounce = false
local DebounceDelay = 0.25

RunService.PreSimulation:Connect(function()
	local Position, PlayerCanSee = Camera:WorldToScreenPoint(Stan.HumanoidRootPart.Position)

	if not PlayerCanSee then
		local Result = workspace:Raycast(Camera.CFrame.Position, Camera.CFrame.LookVector * 2^32)
		
		if not Result or Result and Result.Instance:IsA "BasePart" and Result.Instance ~= workspace.Terrain and Result.Instance.Transparency < 1 then
			if not Debounce then
				Debounce = true

				local CharacterPivot = Character:GetPivot()
				Stan:PivotTo(CharacterPivot * CFrame.new(0, 0, 2.5))

				task.wait(DebounceDelay)
				Debounce = false
			end
		end
	end
end)
2 Likes