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

yeah, but with a small delay so that you can look at him

That doesn’t explain anything.

i believe mine should work
30char

So you want him to move behind the character smoothly or do you want him to teleport constantly with a set delay between each?

tried it, glitches Stan

///////////////

constantly with set delay

////

if the player turns, the z axis will no longer be behind but rather to the side

no, no. it works

/////////////

You could fit a debounce to it.

local Debounce = false
local DebounceDelay = 0.25

game["Run Service"].Stepped:Connect(function(DeltaTime)
	if not Debounce then
		Debounce = true

		-- Code

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

for a delay, instead of runservice try using a while true do loop, and at the end of the loop put task.wait(number)

and where in the script do i put this?

If you give me your code I could edit to add my part.

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
	end

	if not PlayerCanSee then
		local CharacterPivot = game.Players.LocalPlayer.Character:GetPivot()
		Stan:PivotTo(CharacterPivot * CFrame.new(0, 0, 2.5))
	end
end)
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
		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)

works

/////////////////////////

1 Like

but one last problem, he can see through walls

You could cast a ray from the camera position to the NPC position and if nothing is returned or if a result is returned but the part is seethrough you follow through. I’ll edit the code I gave you and give you the end result in a minute, tell me if it works.

im going to sleep, in the morning ill try the code you sent.

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)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.