Instance:Remove() and returning it doesn't return its children

So I’m trying to make the hat invisible when I go in first-person, but the only way I was able to do so was by doing :Remove(), but when I return it, the hat’s children are gone, resulting in the hat not being there and the camera glitching out.
It’s in the hidehats function in the code:

wait(0.5)
local handle = script.Parent.Handle
local char = game.Players.LocalPlayer.Character
local camera = workspace.CurrentCamera
local playeer = game.Players.LocalPlayer
seat = game.Players.LocalPlayer.Character.looke
local RunService = game:GetService("RunService")

function hideHats()
	for _, v in pairs(char:GetChildren()) do
		if v.ClassName == "Accessory" and v.AccessoryType == Enum.AccessoryType.Hat then
			
			v:Remove()
			print("hat")
			wait(3)
			for _, child in pairs(v:GetChildren()) do
				child.Parent = char
				print(child.Parent)
			end
			--v.Parent = char
		end
	end
end

script.Parent.Equipped:Connect(function()
	--seat = script.Parent.Parent["Right Arm"]
	char.Torso["Right Shoulder"].Enabled = false
	char.Torso.alig.Enabled = true
end)

script.Parent.Unequipped:Connect(function()
	char.Torso["Right Shoulder"].Enabled = true
	char.Torso.alig.Enabled = false
end)


RunService.RenderStepped:Connect(function()
	local camCF = camera.CFrame
	seat.CFrame = camera.CFrame - camera.CFrame.Position + seat.Position;
	--seat.CFrame = CFrame.new(seat.Position, camera.CFrame.Position)*CFrame.Angles(0, math.rad(180), 0)
end)

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		camera.CameraSubject = char.CamSub2
		char.Torso.Bodyturn.Enabled = true
		hideHats()
		camera.FieldOfView = 90
		playeer.CameraMinZoomDistance = 0.6
		playeer.CameraMaxZoomDistance = 0.6
		char.turne.Turn.TargetAngle = 45
		--handle.Turn.TargetAngle = 47
	end

end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		camera.CameraSubject = char.CamSub1
		char.Torso.Bodyturn.Enabled = false
		camera.FieldOfView = 60
		playeer.CameraMaxZoomDistance = 5
		playeer.CameraMinZoomDistance = 5
		char.turne.Turn.TargetAngle = 0
		--handle.Turn.TargetAngle = 0
	end

end)
RunService:BindToRenderStep("MouseLock",Enum.RenderPriority.Last.Value+1,function()
	userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
end)


I don’t actually know what I’m doing in the function at all fyi

1 Like

is tIs this a localscript or serversided script?

Its actually in a local script inside of a tool, but it being in a tool doesn’t seem to affect anything, so its basically as if I’m putting it in starterCharacterScripts

Use LocalTransparencyModifier to make everything in your character invisible instead of removing hats. This is the same property that the stock first person mode script uses to make the character invisible.

2 Likes

FYI, to answer the actual title of this topic (which I forgot about when I saw the actual issue):

When you :Delete() or :Destroy() an instance, its Parent will be set to nil… and all of its children will also be :Delete()d or :Destroy()ed.
Therefore, your object will lose all of its children.

:Delete() and :Destroy() imply that you will never use the instance again.

Had you simply done v.Parent = nil instead of v:Remove(), your hat would’ve stayed intact during its little out-of-world experience.

Furthermore, :Delete() has been deprecated in favor of :Destroy() for like a decade now.

Please use

v:Destroy()

instead of

v:Remove()