Attempt to index nil with character properties in a local script

Hi there. I am currently working on a viewport character system, in which the player’s character gets cloned to a viewport frame.

Continuing on, I am having a slight problem with getting it working. I’ll show the code and explain it below.

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

    local viewPortFrame = script.ViewportFrame

    local function addToViewPort()

	local newViewPortFrame = viewPortFrame:Clone()
	newViewPortFrame.Name = char.Name
	newViewPortFrame.Parent = workspace.TeamSelect.TeamSelectHumRoot.SurfaceGui

	local newChar = char:Clone()
	newChar.Parent = viewPortFrame

	local camera = Instance.new("Camera", workspace)
	camera.CFrame = CFrame.new(newChar.PrimaryPart.Position + (newChar.PrimaryPart.CFrame.lookVector * 3), newChar.PrimaryPart.Position)
	camera.Parent = newViewPortFrame

end

The only lines of code not working are the lines that change character’s properties, and the viewport frame’s name is actually being renamed to the character’s name, which means the character isn’t a nil game object.

I’ve searched for solutions on discord, roblox developer hub, etc. Nothing comes up.

(And yes, I do call the function later in the code, so don’t ask me about that)

1 Like

Which line is erroring here? Attempt to index nil with x usually means that the thing you’re indexing is nil.
Example:

local character = player.Character
print(character.PrimaryPart) -- Will error if the variable character is ni

I’ve already explained that the lines erroring are when I try and change the characters properties. When I localize the character, it doesn’t give me an error, and it actually changes the viewport’s name to the character’s name, which means the character isn’t nil.

I’ll go ahead and test the code you sent.

Yep. It didn’t error, it just printed nil. I don’t know why, but it doesn’t work.

Maybe try printing the name of the primary part, also is the primary part set?

Look at the message above, I did that. And yes, the primary part is set.

1 Like

What part of the code is where you change the char’s properties?

Well, I fixed it, although I’m still getting an error. I had to localize the character in the function, not the start of the script.

What are your errors in the output now?

The same attempt to index nil one.

Roblox makes no sense.

It prints the character’s Humanoid Root Part but the character is still nil?

I did:

print(char.Parent)

And it’s even printing the character’s parent as workspace… If the character is a valid game object (not nil), then how is it erroring as nil?

Can I please see line 47 of the script? That seems to be where the error is happening.

local newChar = char:Clone()
newChar.Parent = viewPortFrame

For reference it’s located inside of the “addToViewPort” function.

1 Like

Do you mean to put newChar in viewPortFrame or do you actually want to put it in newViewPortFrame?

I do in fact want the ‘newChar’ inside of the viewport frame.

1 Like

Maybe just do “player.Character” instead of “player.Character or player.CharacterAdded:Wait()” when you define the character.

Did that, although using “player.Character or player.CharacterAdded:Wait()” is more secure in making sure the script doesn’t run before the character get’s added.

1 Like

i think newChar might be nil, by default the characters have its Archivable property to false, meaning it cannot be cloned, try setting the Archivable to true, then try cloning it :thinking:

3 Likes