Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I fix this "Attempted to index nil with 'Parent'."?

I've been working on a project that uses viewport frames to show the world around it (Kind of like how a mirror would work) and I keep getting this error. It works for everything inside the game except cloning a players character. I have tested models other than the player too and they work fine.

The line with the error: v:Clone().Parent = script.Parent.Parts;

Also I have done some debugging and its not the script.Parent part, its the v:Clone().Parent part.

Full script:

local viewportFrame = script.Parent;

local part = workspace.Mirror;
 
local viewportCamera = Instance.new("Camera");
viewportFrame.CurrentCamera = viewportCamera;
viewportCamera.Parent = viewportFrame;

repeat wait() until game.Players.LocalPlayer.Character;
repeat wait() until game.Players.LocalPlayer.Character.Head;
 
viewportCamera.CFrame = CFrame.new(part.Position, part.Position+part.CFrame.LookVector);

game:GetService("RunService").RenderStepped:Connect(function(step)
	for i,v in pairs(script.Parent.Parts:GetChildren()) do
		v:Destroy();
	end;
	for i,v in pairs(workspace:GetChildren()) do
		if (not v:IsA("Terrain") and not v:IsA("Camera")) then
			local ln = "";
			local s,e = pcall(function()
				ln = v.Name;
				v:Clone().Parent = script.Parent.Parts;
			end);
			
			if (not s) then warn(ln.. " cannot be re-parented || error || ".. e) end;
		end;
	end;
end);

1 answer

✓ Accepted answer

The reason may be that your not referencing the parts position in this case it would be the workspace. Keep in mind that instancing a part is also useful without changing its property. Also there's no need to use the ; it mostly use in tables and dictionaries Try this

v:Clone().Parent = game.Workspace 

5 comments

0

this is under runservice renderstepping so that will cause tons of lag. Im cloning items in workspace to the viewport frame.

0

I also use ; for js and the habit goes over

0

But its commonly used in tables or dictionaries. But you can use it if you want.

0

well it still doesn't fix the issue I usually can fix this but it only happens with the player's character. Is it because it is a character of a player?

0

Oh it's because the player's character is not Archivable. I just need to set that to true!

This discussion is preserved as it was. New answers, comments, and votes are closed.