Go back to player's character and camera when key pressed again

hello! I have a script that makes my player go to the npc, idk how would I make it so that it detects if the player’s character is in the NPC, and if yes, then the player’s character go back to the player

My robot man script:

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Npc = game.Workspace.Npc
local camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer

UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end
	local Player = game.Players.LocalPlayer
	if Key.KeyCode == Enum.KeyCode.E then
		camera.CameraSubject = Npc.Humanoid
		Player.Character = Npc

	end
	
end)

Please help i do not know how to get the player’s camera

What does this mean?

And this?

means that the player’s character = the npc

that means the player’s character = the player

Okay, it would help if you explained what this script is supposed to do. I’m assuming you want to be able to press E once, transfer control to the NPC, press E again, and transfer control back to your normal character?

that is right, not really sure how to do that tho

i tried this

if Key.KeyCode == Enum.KeyCode.E and Player.Character = Player.character then
		camera.CameraSubject = Npc.Humanoid
		Player.Character = Npc

	end
if Player.Character == Npc then
  -- the player's character is the NPC
else
  -- the player's character is not the NPC
end

I’m not sure, but I think you should change the Character from the server.
And you must clone the character so that it is not destroyed.
(remember to activate .Archivable before cloning it)

Camera has a CFrame value in it. You could try setting that value to the CFrame of the Npc’s humanoidrootpart and then you can add some studs so its not just right up against the npc.

Well now that wouldn’t make much sense would it?

You’re asking “is 2 equal to 2” :slight_smile:

UMM…now it just make my avater in the npc when i joined now…

if Key.KeyCode == Enum.KeyCode.E and Player.Character == Npc then
		camera.CameraSubject = Player
		Player.Character = Player
	else
		camera.CameraSubject = Npc
		Player.Character = Npc
	end

if Key.KeyCode == Enum.KeyCode.E and Player.Character == Player.HumanRootPart then
		camera.CameraSubject = Npc
		Player.Character = Npc
	else
		camera.CameraSubject = Player
		Player.Character = Player
	end