Viewportframe - Attempt to index nil with 'Parent'

i am making a gui that display’s my character and if it changed it destroy’s the old model and clones the new one. but i get a output error and it is strange.

image

i can’t find the reason. is it possible to get help?

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local VF = script.Parent

function Clone()
	local ClonedCharacter = Character:Clone()
	
	ClonedCharacter.Parent = VF.WorldModel
	ClonedCharacter.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	ClonedCharacter:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,-9.5),Vector3.new(0,0,0)))
end

function destroy()
	local Model = VF.WorldModel[player.Name]
	Model:Destroy()
	
	local Cam = VF.Camera
	Cam:Destroy()
end

function cam()
	local cam = Instance.new("Camera"); cam.Parent = VF
cam.CFrame = CFrame.new(0,0,0)
VF.CurrentCamera = cam
end

repeat wait(.1) until game:IsLoaded()

if Character.ChildAdded or Character.ChildRemoved then
	if VF.WorldModel:FindFirstChild(player.Name) then
		destroy()
	end
	cam()
	Clone()
end

game.Players.PlayerAdded:Connect(function()
	cam()
	Clone()
end)

hope someone can help me.

1 Like

In your Clone() function, you need to set the cloned character model to enable the archivable setting. It’s turned off by default when you clone a character model.

Code would look like this:

function Clone()
	local ClonedCharacter = Character:Clone()
	ClonedCharacter.Archivable = true --NEW LINE I ADDED

	ClonedCharacter.Parent = VF.WorldModel
	ClonedCharacter.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	ClonedCharacter:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,-9.5),Vector3.new(0,0,0)))
end
1 Like

Yes, player character has its Archivable property set to false and if you want to :Clone() it You need to set Archivable to true before cloning, otherwise :Clone() will return nil in that case.

function Clone()
	Character.Archivable = true --Make sure Archivable property is true, otherwise :Clone() will return nil
	local ClonedCharacter = Character:Clone()

	ClonedCharacter.Parent = VF.WorldModel
	ClonedCharacter.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	ClonedCharacter:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,-9.5),Vector3.new(0,0,0)))
end

And also here:

if Character.ChildAdded or Character.ChildRemoved then
	if VF.WorldModel:FindFirstChild(player.Name) then
		destroy()
	end
	cam()
	Clone()
end

That’s not how You connect code to events. You need to write a function and then connect it to events using :Connect()

1 Like