Script:7: attempt to index nil with 'Parent'

For some reason when I try to change the clone’s parent it gives me this error

 local char

script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr,mode)
	char = plr.Character
	if mode == "fakeclone" then
		local clone = char:Clone()
		clone.Parent = workspace
		
		char:PivotTo(char.HumanoidRootPart.CFrame * CFrame.new(0,0,3) )
		
		for i,v in pairs(char:GetDescendants()) do
			if v:IsA("BasePart") or v:IsA("MeshPart") then
				v.Transparency = 1
			elseif v:IsA("Decal") then
				v.Transparency = 1
			end
		end
		
		clone:FindFirstChildOfClass("Humanoid").Died:Once(function()
			for i,v in pairs(clone:GetChildren()) do
				if v:IsA("BasePart") or v:IsA("MeshPart") then
					v.Anchored = true
					v.Color = BrickColor.new("Really red")
					v.Material = Enum.Material.Neon
				end
			end
			local explosao = Instance.new("Explosion",workspace)
			explosao.Position = clone:FindFirstChild("Head")
			explosao.BlastRadius = 20
			explosao.BlastPressure = 1500
		end)
		
		task.wait(10)
		
		clone:Destroy() 
		
		for i,v in pairs(char:GetDescendants()) do
			if v:IsA("BasePart") or v:IsA("MeshPart") then
				v.Transparency = 1
			elseif v:IsA("Decal") then
				v.Transparency = 1
			end
		end
		
	end
end)
1 Like

Instance.Archivable

“This property determines whether the instance should be included when the experience is published or saved, or when Clone() is called on one of the instance’s ancestors. Calling Clone() directly on an instance will return nil if that instance is not Archivable.”

By default, a player’s character has its “Archivable” property set to false. You will need to enable it before cloning.

P.S. The MeshPart class is a descendant of the BasePart class. Instance:IsA matches not only the given class, but all descendants of that class. Since you use IsA on the “BasePart” class, an additional check for the “MeshPart” class is redundant. Furthermore, the majority of your function’s code is indented by one if-statement. This is harmful towards readability. You can avoid indenting the majority of your function by invereting your if-statement:

if mode ~= "fakeclone" then
    return
end

-- ...

This logic can also be applied to the body of your loops

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.