Detect if a player is in npc

I want it so that when the player presses e, the server checks if the plyer’s character is in the NPC, if yes then change the camera and the character to the Player
ma script:

local Character = script.Parent
local Npc = game.Workspace.Npc
local camera = workspace.CurrentCamera


UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end

	if Key.KeyCode == Enum.KeyCode.E then
		local Player = game.Players.LocalPlayer
		camera.CameraSubject = Npc.Humanoid
		Player.Character = Npc	

	end
end)
1 Like

A server script wouldn’t work because you’re modifying from a local script, which the server won’t see changes to.

if camera.CameraSubject == Npc.Humanoid then
   camera.CameraSubject = Character.Humanoid
   Player.Character = Character
else
   camera.CameraSubject = Npc.Humanoid
   Player.Character = Npc
end

If you absolutely need the server to know the updates you can use RemoteEvent. Then the server is updated that the player switched from Npc to Character or vice versa.

hmm… where would i put the script? after the first end?

Updated my reply. Hope it helps

like this?

local Character = script.Parent
local Npc = game.Workspace.Npc
local camera = workspace.CurrentCamera


UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end

	if Key.KeyCode == Enum.KeyCode.E and camera.CameraSubject ~= Npc.Humanoid then
		local Player = game.Players.LocalPlayer
		camera.CameraSubject = Npc.Humanoid
		Player.Character = Npc
	else
		camera.CameraSubject = Character

	end
	
end)

No the if statement I gave would have to be inside the if statement for the keybind. Just a quick explanation on why this wouldn’t work…

The else statement would be anything that’s not the E key. Meaning that basically if you press anything that’s not E will run the else statement.

So, instead using your original code, here’s the excerpt of the only parts i modify of your code…

if Key.KeyCode == Enum.KeyCode.E then
   local Player = game.Players.LocalPlayer
   if camera.CameraSubject == Npc.Humanoid then
      camera.CameraSubject = Character.Humanoid
      Player.Character = Character
   else
      camera.CameraSubject = Npc.Humanoid
      Player.Character = Npc
   end
end

Disclaimer I’ve never tried overwriting the Player.Character property so I’m not entirely sure if that’s valid code, but this should work as you intend otherwise.