No Hat/Accessories Script

You can write your topic however you want, but you need to answer these questions:
1.A Script That Does Not Allow Hats?

  1. Needs A Script That Does Not Allow Hats But I Dont Know How To Scrpt?

  2. None I Have None?

2 Likes

you can try to get the character and loop through it and delete the hat accessories

for _,v in pairs(character:GetChildren()) do 
	if v:IsA("Accessory") then 
		v:Destroy()
	end
end
1 Like

(ty mrloney)
put in serverscriptservice

game:GetService("Players").PlayerAdded:Connect(function(plr)
     plr.CharacterAppearanceLoaded:Connect(function(char)
          for _, v in pairs(char:GetChildren()) do
               if v:IsA("Accessory") then
                    v:Destroy()
               end
          end
     end)
end)
4 Likes

This would sometimes not remove the hats, because even if the character is added, it doesn’t mean the appearance is loaded. I had that problem while trying to check for a face covering while making a game jam game this year.

I would suggest this method Player | Documentation - Roblox Creator Hub

Edit:
Funny enough, this is the code Roblox provides on that wiki page.

local Players = game:GetService("Players")
 
local function playerAdded(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
 
		-- save hats for later
		local accessories = {}
		for _, accessory in pairs(humanoid:GetAccessories()) do 
			table.insert(accessories, accessory:Clone())
		end
 
		-- remove hats
		humanoid:RemoveAccessories()
 
		wait(5)
 
		-- make sure the player still exists, and has the same character
		if player and player.Character and player.Character == character then
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			if humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
				-- give back the hats
				for _, accessory in pairs(accessories) do 
					humanoid:AddAccessory(accessory)
				end
			end
		end
 
		accessories = nil
	end)
end
 
-- get existing players
for _, player in pairs(Players:GetPlayers()) do 
	playerAdded(player)
end
-- listen for new players
Players.PlayerAdded:Connect(playerAdded)
2 Likes

I tried this earlier not sure how it works do I have to edit the script?

If you don’t plan on giving the hats back.
Just use this function.

1 Like

Ok I will try it thanks for the help

Do I have to put it in the server script service

Yes

game:GetService("Player").PlayerAdded:Connect(function(player)
  player.CharacterAppearanceLoaded:Connect(function(character)
    local hum = character:WaitForChild("Humanoid");
    if not hum then return; end;
    hum:RemoveAccessories();
  end);
end);
2 Likes

Do I have to change anything to the script

Do i have to put it in a model

You can just drop it into ServerScriptService in a Script.

1 Like

I tried that and it did nothng

I just used this in studio to test and it worked

--// Name: Script.lua
--// Author: Danny (MrLonely1221)
--// Description: 

--// Variables


--// Functions


--// Events
game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid");
		if humanoid then
			humanoid:RemoveAccessories();
		end;
	end);
end);

2 Likes

Do you need to use a module script or a normal script

Nevermind Its Perfect Tysm For this

1 Like