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

How to position player hats?

game.Players.PlayerAdded:connect(function(player)
	player.CanLoadCharacterAppearance = false
	local characterArmour = script["Grim Reaper"]
	if characterArmour then
		player.CharacterAdded:connect(function(character)
			if character then
				for _, v in pairs(characterArmour:GetChildren()) do
					if v.Name == "Handle" then
						local handle = v:Clone()
						local accessory = Instance.new("Accessory", character)
						handle.Parent = accessory
						handle.FormFactor = "Custom"
						handle.Size = Vector3.new(2, 1, 1)
						accessory.AttachmentPos = Vector3.new(0, 0.5, 0)
						handle.Anchored = false
					end
				end
			end
		end)
	end
end)

I'm trying to get a part to be attached to the players head, like a hat. The only problem I have is when I change the handle to something else, it changes how it's placed on the character. How can I make it so it always put the handle on the players head as if it were a normal hat?

EDIT Images

1 answer

✓ Accepted answer

Instead of over-complicating things, you should instead use InsertService.

This will take the accessory straight from the catalog and right into the player. The script would look a little bit like this.

game.Players.PlayerAdded:connect(function(p)
	repeat wait() until p.Character -- (WAITING FOR CHARACTER)
	local hatId = 28276664 -- Change to whatever you want it to be
	local insertService = game:GetService("InsertService") -- Getting insertService
	insertService:LoadAsset(hatId):GetChildren()[1].Parent = p.Character -- Loading it  into the character
end)

The script above works because it's getting the accessory and placing it straight into the character. The accessory already comes with all the positioning, so all you really need to do is that.


If you want to insert multiple hats at the same time, you can do so like this

local hats = {11748356,1235488,124730194} -- Accessories IDS go in here
game.Players.PlayerAdded:connect(function(p)
	repeat wait() until p.Character -- (WAITING FOR CHARACTER)
	local insertService = game:GetService("InsertService") -- Getting insertService
	for i,v in pairs(hats) do
		insertService:LoadAsset(v):GetChildren()[1].Parent = p.Character -- Loading in the hats
	end
end)

For a custom hat, you would need the hat to be somewhere in the game, and cloning it right into the player

game.Players.PlayerAdded:connect(function(p)
	repeat wait() until p.Character -- (WAITING FOR CHARACTER)
	script:WaitForChild("ClockworksShadesYellow"):Clone().Parent = p.Character -- Cloning hat into the player
end)

The hat as you can see is already inside the script, so we're just cloning it.

And it all works, perfectly PLAYER WITH THE CUSTOM HAT

2 comments

0

I mean, it kinda works, but I'm using custom hats, and when I load them in, they dont position correctly

0

I'll edit my question, I have positioned the hats to a player head on a model. I clone the hat and the head into the player. How can I get the hat to be cloned to the same position as the 'fake' head? You can see the hat spawns above the players head. I know I could adjust the attachmentpos, but its different for each hat. I just want every hat to spawn on the same position for every player

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