Equipping a hat with a Button problem

What do you want to achieve?
I’m trying to make a equip-a-hat using a UI Button

Video and Picture
GIF
https://gyazo.com/fa1e5ec55eafd9f2def0c767951f2b05
Properties of the attachment in case you want to see it

What solutions have you tried so far?
I tried parenting it, positioning and using :AddAccessory()

Whats the issue?
If I click the button it spawns on the SpawnLocation and does not attach to the player

Code

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AccessoriesFolder = ReplicatedStorage:WaitForChild("Accessories", 1)
local HatFolder = AccessoriesFolder:WaitForChild("Hats")
local DinoFolder = HatFolder:WaitForChild("Dino")

local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character
local Humanoid = Character:WaitForChild("Humanoid")

local RedDinoHat = DinoFolder.RedDino

local Button = script.Parent

Button.MouseButton1Click:Connect(function()
	local CloneHat = RedDinoHat:Clone()
	CloneHat.Name = "Hat1"
	--CloneHat.Parent = Character
	Humanoid:AddAccessory(CloneHat)
end)

The part for the mesh (not the accessory) should be named Handle did you put the part name to Handle?

1 Like
local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AccessoriesFolder = ReplicatedStorage:WaitForChild("Accessories", 1)

local HatFolder = AccessoriesFolder:WaitForChild("Hats")

local DinoFolder = HatFolder:WaitForChild("Dino")

local LocalPlayer = Players.LocalPlayer

local Character = LocalPlayer.Character

local Humanoid = Character:WaitForChild("Humanoid")

local RedDinoHat = DinoFolder.RedDino

local Button = script.Parent

Button.MouseButton1Click:Connect(function()

   local CloneHat = RedDinoHat:Clone()

   CloneHat.Name = "Hat1"

   --CloneHat.Parent = Character

   CloneHat.Handle.Position = LocalPlayer.Character.Head.Position

   Humanoid:AddAccessory(CloneHat)

   CloneHat.Handle:WaitForChild("AccessoryWeld").Part1 = Character.Head

end)

I tested it in studio and it works! (Make sure you add a weld inside of the handle and make part0 the handle)

How the hat should look like:

What else you need to do is to make sure the Handle is uncollideable

4 Likes

Yes, I used Accessory Inserter for that but I just need to put a weld

The hat positions like this

For me it worked fine. It’s probably not the script. I’ll try to figure it out.

Now I see what you was talking about…

I managed to fix the script but the only problem is that I’m not sure if it’s gonna work on certain avatars and hats.

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AccessoriesFolder = ReplicatedStorage:WaitForChild("Accessories", 1)

local HatFolder = AccessoriesFolder:WaitForChild("Hats")

local DinoFolder = HatFolder:WaitForChild("Dino")

local LocalPlayer = Players.LocalPlayer

local Character = LocalPlayer.Character

local Humanoid = Character:WaitForChild("Humanoid")

local RedDinoHat = DinoFolder.RedDino

local Button = script.Parent

Button.MouseButton1Click:Connect(function()

   local CloneHat = RedDinoHat:Clone()
   CloneHat.Name = "Hat1"

   --CloneHat.Parent = Character

   CloneHat.Handle.Position = LocalPlayer.Character.Head.Position

   Humanoid:AddAccessory(CloneHat)

   CloneHat.Handle.AccessoryWeld.Part0 = CloneHat.Handle

   CloneHat.Handle.AccessoryWeld.Part1 = Character:FindFirstChild("Head")

   CloneHat.Handle.AccessoryWeld.C0 = CFrame.new(0,-0.7,-0.35)

end)

R6:

R15:

As you can see the R15 one isn’t symmetrical due to a different head.

I think what you can do is to modify the last line of the script, which is this line:

CloneHat.Handle.AccessoryWeld.C0 = CFrame.new(0,-0.7,-0.35)

If you modify the CFrame for a certain head, what makes me come out with an idea of making a specific table, that stores all the CFrame data for each head, and accessory if needed.

I just need some time to make the table.

1 Like