How to parent hats off a character

Why hello again. Yes I ask again a lot.

Don’t be scared by my code, couldn’t find anything else to do when setting the transparency of everything.

Here’s my script, I will explain after:

local tool = script.Parent

local character = tool.Parent

local Dissapear = tool:WaitForChild("Dissapear")

local debounce = false

Dissapear.OnServerEvent:Connect(function(player, character)
	for _, Hat in pairs(character:GetChildren()) do
		if Hat:IsA('Accessory') and debounce == false then
			task.wait(1)
			Hat.Parent = game.ServerStorage
			local sound = Instance.new("Sound", tool.Handle)
			sound.SoundId = "rbxassetid://835453268"
			sound.Volume = 1
			debounce = true
			
			--player INVISIBLE!
			
			local head = character:WaitForChild("Head")
			head.Transparency = 1
			head:WaitForChild("face").Transparency = 1
			character:WaitForChild("Torso").Transparency = 1
			character:WaitForChild("Left Arm").Transparency = 1
			character:WaitForChild("Right Arm").Transparency = 1
			character:WaitForChild("Left Leg").Transparency = 1
			character:WaitForChild("Right Leg").Transparency = 1
			wait(1)
			Hat.Parent = tool.Parent
			debounce = false
			sound:Destroy()
			
			--player is visible again
			local head = character:WaitForChild("Head")
			head.Transparency = 0
			head:WaitForChild("face").Transparency = 0
			character:WaitForChild("Torso").Transparency = 0
			character:WaitForChild("Left Arm").Transparency = 0
			character:WaitForChild("Right Arm").Transparency = 0
			character:WaitForChild("Left Leg").Transparency = 0
			character:WaitForChild("Right Leg").Transparency = 0
		end
	end
end)

So basically it doesn’t print out errors, but it doesn’t make the player Invisible, nor does it parent the players hats into ServerStorage. It just deletes the hats. And I need a better way of doing this.

I’ve tried doing :GetService(“ServerStorage”) and it didn’t work, same thing happens. No errors at all.

(If you are still not annoyed with me flooding your page with nothing but failed projects lol) I appreciate your help if you do.

Do you know if your code is really running? It might not be, try putting a print statement inside the code to make sure it is. I would put one directly at the top of the event’s code. If it doesn’t print then it might be an issue with where the script is located and what type it is.

1 Like

Oh yea… It isn’t running, where should I put my script?

Note: Its in the handle and nowhere else

So the problem is that when the tool is unequipped Roblox puts it and the script inside the Player’s backpack folder so it won’t run the script.

Also note that I am talking specifically about server scripts, I am not sure if localScripts run when they are in the player but I know localScripts at least run inside the player character. Maybe consider changing the code so it comes from a client script or outside the entire player character’s tool all together.

It’s not meant to be unequipped. I don’t think I made it unequip itself.

I can’t figure out how to get the character from that really

From any script (client or server) you can use a property that directly defines the character, “Character” it is a part of the player instance.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAppearanceLoaded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		for _, Accessory in ipairs(Humanoid:GetAccessories()) do
			local Handle = Accessory:FindFirstChild("Handle")
			if Handle then
				Handle:PivotTo(CFrame.new(0, 0, 0))
			end
			Accessory.Parent = workspace
		end
	end)
end)

This relatively primitive script is working for me.