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

Hat remove on PlayerAdded script?

Hi all! So, for my game, I've wanted to create a Hat Giver according to if they have a gamepass or not! But, with this summons a new problem; too many hats! I would like to know how to remove hats upon entering a game, and if they player is a boy, give them a specific hair, and if the player is a girl if them a specific hair!

I've yet to attempt a code for this, because I wouldn't even know where to begin, so anything is help! Thank you guys!

2 answers

✓ Accepted answer

Server Script

local Market = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		-- Checks if Player has Gamepass (Change 0 to the Gamepass ID)
		if Market:UserOwnsGamePassAsync(Player.UserId, 0) then
			wait(0.1)
			-- Removes Previous Hair
			for i, v in pairs(Character:GetChildren()) do
				if v:IsA("Accoutrement") then
					v:Destroy()
				end
			end
		-- Creates a New Accessory
			local newaccessory = Instance.new("Accessory")
			newaccessory.Name = "NewHair"
			
			local handle = Instance.new("Part")
			handle.Name = "Handle"
			handle.CanCollide = false
			handle.Anchored = false
			handle.Size = Vector3.new(1, 1, 1)
			handle.Parent = newaccessory
			
			local weld = Instance.new("Weld")
			weld.Parent = handle
			weld.Part0 = handle
			weld.Part1 = Character:WaitForChild("Head")
			
			local mesh = Instance.new("SpecialMesh")
			mesh.Scale = Vector3.new(1, 1, 1)
			mesh.Parent = handle
		-- Checks Gender based on if the player owns an asset
		-- Change the id of the below (mesh / texture / cframe offset] for the new hair
			if Market:PlayerOwnsAsset(Player, 63690008) then -- Default Boy Hair
				mesh.MeshId = "http://www.roblox.com/asset/?id=0"
				mesh.TextureId = "http://www.roblox.com/asset/?id=0"
				weld.C0 = CFrame.new(0, 0, 0)
			elseif Market:PlayerOwnsAsset(Player, 451220849) then -- Default Girl Hair
				mesh.MeshId = "http://www.roblox.com/asset/?id=0"
				mesh.TextureId = "http://www.roblox.com/asset/?id=0"
				weld.C0 = CFrame.new(0, 0, 0)			
			end
		end
	end)
end)

Explanations of the functions are written in the script

The player.CharacterAdded Event is used to get the player's Character when they join the game

There is no property to determine gender, but players are also unable to remove the default hairs from their inventory, the one's given when they join roblox based on their gender choice

2 comments

0

This isn't working. My code is the same, I added the Texture/Mesh ID's for 'Cinnamon Hair" For girls, and "Beautiful Hair for Beautiful People" For boys, and doesn't work.

0

You can change the word "Accoutrment" to "Accessory" and see if that helps, also, make sure to change the C0 CFrame, this code will create the accessory, but its position may be inside the player's head

Retrieving a player's gender is not possible in Roblox because it's illegal to give out personal information like that.

As for removing hats upon entering the game, you can use the following code :

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		wait() -- giving a bit of time for the hats to load
		for i,v in pairs(character:GetChildren()) do
			if v:IsA("Accoutrement") then
				v:Destroy()
			end
		end
	end)
end)

Hope this helps!

3 comments

0

-1. Using deprecated "connect"

0

Change it to :Connect.

0

Both "connect" and "Connect" have the same use, even though it's deprecated.

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