Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why isn't this remove hat script working?

This script is supposed to remove all hats from a player when a player joins:

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		local d = character:GetChildren()
		for i = 1, #d do
			if d[i].ClassName == "Hat" then
				d[i]:Destroy()
			end
		end		
	end)
end)

It doesn't work, though. Any help?

2 comments

0

This is kinda a request, figure out where the error is next time and then we can fix that error.

0

This question is fine, the code is both simple and short. If it was like 100 lines then you'd be totally right, though.

1 answer

Your code isn't working because hats don't get added with the character, there is sometimes some delay between the character model being added and the hats being added to that mode. This code will avoid that delay:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
		character.ChildAdded:connect(function(child)
			if child:IsA("Hat") then
				wait()
				child:Destroy()
			end
		end) 
    end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.