Error in my script

i have a script that is supposed to make player go to the npc’s character and go back if the player pressed “e” and is in npc’s character…or it’s supposed to do that…
script

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

UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end
	if Key.KeyCode == Enum.KeyCode.E and Player.CameraSubject = Npc.Humanoid then --the error
		camera.CameraSubject = Npc
		Player.Character = Npc
	else
		Player.Character = Player.HumanoidRootPart
		camera.CameraSubject = Player.Humanoid
		
	end

	

end)

Player.CameraSubject = Npc.Humanoid

Since it’s an if statement, you need two ='s.

2 Likes

it says i don’t have a humanrootpart which I clearly have


it does this everytime i press a key

It’s Player.Character.HumanoidRootPart

2 Likes

oh
Screen Shot 2021-05-25 at 8.35.10 AM
like this?:

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
	if Key.KeyCode == Enum.KeyCode.E and Player.Character == Player.Character then
		camera.CameraSubject = Npc
		Player.Character = Npc
	else
		Player.Character = Player.Character.HumanoidRootPart
		camera.CameraSubject = Player.HumanoidRootPart
		
	end

	

end)

Why are you trying to set Player.Character to Player.HumanoidRootPart? Also there are several major errors in your code.

uhh i want the player go back to the player’s oringinal character…what do i set it in other than HumanoidRootPart

uhh i want the player go back to the player’s oringinal character…

What does this exactly mean?
What’s the goal of this script? Are you trying to make it so you can chat to NPCs?

no, what am i doing in the script is that when i press e i go to the npc’s character, and when i press e again i go back to the character of me(aka player)

Are you trying to manipulate the camera?

well kinda yes the camera is switching between the player and the Npc

Okay, here’s a better script for you.

local UIS = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer -- references the player
local Character = Player.Character or Player.CharacterAdded:Wait() -- references the players character
local Camera = game.Workspace.CurrentCamera -- references the camera

local NPC = game.Workspace.Npc
local LOL = game.Workspace.Lol

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

    if Key.KeyCode == Enum.KeyCode.E and Camera.CameraSubject == Npc:FindFirstChild("Humanoid") then
        Camera.CameraSubject = Character:FindFirstChild("Humanoid")
    elseif Key.KeyCode == Enum.KeyCode.E and Camera.CameraSubject == Character:FindFirstChild("Humanoid") then
        Camera.CameraSubject = Npc:FindFirstChild("Humanoid")
    end
end)

Put this script into StarterPack or StarterPlayerScripts.

I don’t exactly why you are trying to set Player.Character to Player.HumanoidRootPart because that won’t do anything, especially since you’re trying to set something to it’s own child.

This should be NPC (mini question…how would i make the npc have a blank name but I it be npc in the workspace

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

UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end
	if Key.KeyCode == Enum.KeyCode.E and camera.CameraSubject == Npc.Humanoid then
		camera.CameraSubject = Player.Humanoid
	elseif Key.KeyCode == Enum.KeyCode.E then
		camera.CameraSubject = Npc.Humanoid
		
	end

	

end)

I assume you are running this on the client.

  1. Player.CameraSubject does not exist
  2. Player.Character is read-only
  3. You are setting Camera.CameraSubject to the NPC while checking if Camera.CameraSubject is the NPC
  4. You are setting Camera.CameraSubject to the NPC, not the humanoid of the NPC
  5. You are setting Camera.CameraSubject to a nil value (Player.Humanoid). Use Player.Character.Humanoid, instead of Player.Humanoid
  6. You are setting Camera.CameraSubject to the NPC if the subject is already the NPC

i want to set the player.charater so that the player can move in the npc’s body, not just the camera

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Npc = game.Workspace.Npc
local camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Lol = game.Workspace.Lol
local deb = true
UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end
	if Key.KeyCode == Enum.KeyCode.E and not deb then
		camera.CameraSubject = Player.Humanoid
	elseif Key.KeyCode == Enum.KeyCode.E and deb then
		camera.CameraSubject = Npc.Humanoid
	end
end)

try this
make sure its a local script in the starter character scripts

Set the NameDisplayDistance of the humanoid to 0
Set the HealthDisplayDistance of the humanoid to 0

try this

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Npc = game.Workspace.Npc
local camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Lol = game.Workspace.Lol
local deb = false
UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted then
		return
	end
	if Key.KeyCode == Enum.KeyCode.E and not deb then
		camera.CameraSubject = Character.Humanoid
	elseif Key.KeyCode == Enum.KeyCode.E and deb then
		camera.CameraSubject = Npc.Humanoid
	end
end)

Also set your cam type to scriptable like this
camera.CameraType = Enum.CameraType.Scriptable
But do not do the camera subject I think it is still broken