Attempt to index nil with 'Character

I ran into error that says

I already searched about this problem but I can’t find any topic that similar. Any help is appriciated :slight_smile: . Here is the script:

local door1 = script.Parent.Left
local door2 = script.Parent.Right

local bil = script.Parent.Tiag.BillboardGui

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local cam = workspace.CurrentCamera

local cutscenecam = workspace.Cutscene.CutsceneCam

local TS = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

local ti = TweenInfo.new(2)

local goal = {}
goal.CFrame = cutscenecam.CFrame * CFrame.new(502.1, -346.6, -86.1)

UIS.InputBegan:Connect(function(input , gameprocess)
	
	if gameprocess then return end
	
	if input.KeyCode == Enum.KeyCode.E then
		if (character.HumanoidRootPart.Position - door1.Position).Magnitude < 5 then
			cam.CameraType = Enum.CameraType.Scriptable
			cam.CFrame = cutscenecam.CFrame
			
			TS:Create(cutscenecam , ti , goal)
		end
	end
end)

Could you try doing
(you can still use it how you preferably want it.)

local player = game.Players.LocalPlayer
local character = player.Character

If this isn’t the problem let me know, I’m unsure of which error you mean point you mean.

still hs the same error. The error is at this line

local character = player.Character or player.CharacterAdded:Wait()

Is this running on a Local or Server Script?
If it’s a server script, it will not work as the player you use is the local player.

Try changing to a Local Script.

Its serverscript and I use player service like this

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

That wont work unless you do

game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character) follow a local script like daron said.

If you want it to work properly a function needs to be added for the server script.

One questions…do tween service work on localscript??

Yep, although if you’re going on workspace for it, I highly suggest not doing it as other people will not be able to see, but you can check up on tutorials on how to tween it.

You can’t get the local player in a server script. It is in the name, local. TweenService works on local scripts too and you might want to change your script to local script.

alo this line not working. No error or print in the output .

if (character:WaitForChild("HumanoidRootPart").Position - door1.Position).Magnitude < 10 then

What is wrong??

Technically you’ve already loaded the Humanoid, when a character is added in so you could just do findfirstchild,

if (character:WaitForChild("HumanoidRootPart").Position * door1.Position).Magnitude < 10 then

Try that I’m not too sure if it’ll work.

It is because character is nil.
Edit:
Script stops running when it errors trying to get the character.

I put waitforchild and findfirstchild but still nothing happens
also character and humanoidrootpart is not nill . It print the name

Solution is basic, just change script local script and don’t forget that local scripts don’t run on workspace. Put it inside StarterCharacterScripts or StarterPlayerScripts.

Already did that but nothing happens

Firstly, UserInputService is client-side only, so it cannot be used within a server script, consider replacing this with a ClickDetector, as you’d also be able to get the player from the function of when the door is clicked. This also solves your problem of attempting to get the player and character, as I assume many people have already told you, game.Players.LocalPlayer will not retrieve the player within a server script, however the ClickDetector’s function, ClickDetector.MouseClick, is able to retrieve the player through the following:

ClickDetector.MouseClick:Connect(function(player)
       -- your code
end)

The script is local script now

You’d probably want to use RemoteEvents to send the LocalPlayer variable to the server, then calculate the magnitude of the player (HumanoidRootPart) and the door on a server script in order to handle opening and closing it.