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

Ive been looking everywhere for this solution, devforum, youtube, but nothing appears to work for me.
Ive been wanting to make an npc that stops moving when you look at it, and start moving when you look away.

Ive tried following a devforum post, didnt work.
image

also, i am not a very good scripter at all, i just started, so any help would be great

So you only want the NPC to follow if it is on the players screen at all or would you like it to be dependant on the character’s rotation?

players screen

spacefiller, dont flag me >:(

You could probably fire a ray from the player’s head to a certain distance and see if the ray hit the NPC. Probably not the best solution but it’s the only thing I can think of.

Well then, what you’ve done wrong is parse a table intead of a Vector3 position, you should change the line:

local Position, PlayerCanSee = Camera:WorldToScreenPoint(Vector2)

should be changed to …

local Position, PlayerCanSee = Camera:WorldToScreenPoint(NPC.HumanoidRootPart.Position)

That wouldn’t be nearly as effective as WorldToScreenPoint.

do i put workspace.Stan where the “NPC” part is?

Also @BandolPole you should format your posts better for readability sake. Use proper grammar and paste your code surrounded by 3 backticks (```) instead of screenshotting it.

If that’s the variable for your NPC’s character, then yes.

okay, now, how could i make the NPC (Stan) teleport to the player if its not looking?
would i have to make a different post for this?

Teleporting is as easy as NPC:PivotTo(PlayerCharacter:GetPivot()).

but how would i go about teleporting it to the player if its not looking?

if not PlayerCanSee then
   NPC:PivotTo(PlayerCharacter:GetPivot())
end

If statements work by running the code inside of them only if the statement between the if and the then is true. In this case, the statement is not PlayerCanSee which equates to PlayerCanSee == nil or PlayerCanSee == false.

this is hopefully the last thing i ask of you, but what do i put in the PlayerCharacter part?

The players character, presumably this is a LocalScript so what you would put in there is game.Players.LocalPlayer.Character.

Stan appears to still be standing still

Could you show me your code?

(minimum character amount)

local Camera = workspace.CurrentCamera
local Stan = workspace.Stan
local Players = game:GetService("Players")
game:GetService('RunService').Stepped:Connect(function()
	local Position, PlayerCanSee = Camera:WorldToScreenPoint(Stan.HumanoidRootPart.Position)
	if PlayerCanSee then
		workspace.Stan.HumanoidRootPart.Anchored = true
		if not PlayerCanSee then
			Stan:PivotTo(game.Players.LocalPlayer.Character:GetPivot())
		end
	end)

You put the not PlayerCanSee if statement inside of an if statement where the code will only run if PlayerCanSee is true. You need to move it outside, like this:

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

RunService.PreSimulation:Connect(function()
	local Position, PlayerCanSee = Camera:WorldToScreenPoint(Stan.HumanoidRootPart.Position)
	
	if PlayerCanSee then
		workspace.Stan.HumanoidRootPart.Anchored = true
	end
	
	if not PlayerCanSee then
		Stan:PivotTo(Character:GetPivot())
	end
end)

it works, but now he noclips into the floor at times, oh also he noclips right into the player, which flings the player, oh also sometimes he just floats
image