Attempt to index nil with 'Parent'

I was trying to make a health GUI, I’m trying to make a viewport frame camera lerp to a copy of the character, it says I got a the error "attempt to index nil with ‘Parent’ at line 18. Here’s my code

local frame = script.Parent:WaitForChild("ViewportFrame")

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local cam = Instance.new("Camera")
frame.CurrentCamera = cam
cam.CameraType = Enum.CameraType.Scriptable
cam.Parent = script.Parent
cam.Name = "ViewportCamera"
repeat wait() until char.PrimaryPart
cam.CFrame = CFrame.new(char.PrimaryPart.Position + Vector3.new(-2,2,-2),char.PrimaryPart.Position)

local cameraSpeed = 0.6

game:GetService("RunService").RenderStepped:Connect(function()
	local shownChar = char:Clone()
	frame:ClearAllChildren()
	shownChar.Parent = frame
	repeat wait() until shownChar.PrimaryPart
	cam.CFrame = cam.CFrame:Lerp(CFrame.new(shownChar.PrimaryPart.Position + Vector3.new(-2,2,-2),char.PrimaryPart.Position),cameraSpeed)
end)

Help is appreciated!

2 Likes

Hi, this error may be happening because char may not exist. CharacterAdded:Wait() should be preceded by a colon.

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer:CharacterAdded:Wait()
2 Likes

That does literally nothing. The proper syntax for CharacterAdded() uses a period:

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

Now, for the primary question:
@gianmarco2712, is the script local or server? LocalPlayer is only client sided.

2 Likes

The issue you’re having is fixed by making the character archivable!

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait() -- yes, use a period
char.Archivable = true
5 Likes

Is correct, the ‘Archivable’ property defaults to ‘false’ for characters.

2 Likes

To clone a character set the Character.Archivable to true and after cloning return it back to false, to prevent exploiters from being able to “break” it by a way or another

2 Likes