How to make a Click to Remove Hats Block?

Hello Developers,

I am trying to create a block using a ClickDetector, where the player clicks it and it removes their hats from their avatar. Could you please help me with this?

Screen Shot 2021-02-11 at 16.52.39

Thank you!

3 Likes

what do you mean? what are you trying to do? also did you see “Remove Hats” in the part?

if you’re trying to do a hat remover:

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	if player.Character ~= nil then
		for i,v in pairs(player.Character:GetChildren()) do
			if v:IsA("Accessory") then
				v:Destroy()
			end
		end
	end
end)

(Script from part)

this is the part:
image

I hope this can help you

1 Like

First let’s connect to a function when the part gets clicked.

local clickdetector = --Your path here--

clickdetector.MouseClick:Connect(function(player)

end)

Now let’s get the character.

local clickdetector = --Your path here--

clickdetector.MouseClick:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    if character then
        
    end
end)

Now let’s go through every child of the character and see if it’s an accessory, if it is, we’ll delete it.

local clickdetector = --Your path here--

clickdetector.MouseClick:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    if character then
        for _, child in ipairs(character:GetChildren()) do
            if child:IsA("Accessory") then
                child:Destroy()
            end
        end
    end
end)

Edit after 5 minutes:
I’m not sure if the parameter gives the player or character, but I’m assuming it gives the player. Correct me if I’m wrong.

3 Likes

dude that’s not the right way to write scripts…

We’re talking about a ClickDetector, not a Button on a SurfaceGui.

Firstly, you’ll need a ClickDetector.
Secondly, a script that deals with what happens once you click the ClickDetector.

That being said, here’s a simple way to accomplish this:

local ClickDetector = script.Parent


local function MouseClick(Player)
	local Character = Player.Character
	if Character then
		local Get = Character:GetChildren()
		for i = 1, #Get do
			local Target = Get[i]
			if Target:IsA('Accessory') then
				Target:Destroy()
			end
		end
	end
end


ClickDetector.MouseClick:Connect(MouseClick)

You’re welcome — and good luck!
(There are many ways to remove accessories, this is one of them, and it is server-sided.)

Just going to plug in here since I’m seeing the same patterns in each code sample: the Humanoid has a convenient shorthand for removing all accessories, the method RemoveAccessories. This is functionally equivalent to going through the character’s children and calling destroy on Accessory objects.

Only drawback is that this method is dependent on the existence of a Humanoid.

ClickDetector.MouseClick:Connect(function (player)
    local character = player.Character
    if not character then return end

    local humanoid = character:FindFirstChild("Humanoid")
    if not humanoid then return end

    humanoid:RemoveAccessories()
end)
2 Likes

I forgot about that feature, although it’s been a while since I interfered with the player instance — seems like there’s a lot of new stuff worth to check out.

I didn’t know that! Thanks for sharing better and more efficient ways of doing a certain thing.