Hats not cloning onto player's head when button is clicked

Hello! so I have a gui where if a button is clicked, a hat will spawn on the players head. The hat is inside of the player but spawns at 0,0,0 and falls instead of on the players head
this is the script:

local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
local hats = game.ReplicatedStorage.Hats

script.Parent.MouseButton1Click:Connect(function()
	humanoid:AddAccessory(hats.BloxxerCap:Clone())
end)

I can provide any other info if needed!

The issue is probably that player.Character isn’t fully loaded when your script runs. Wrap the humanoid fetch inside the button click like this:

local player = game.Players.LocalPlayer
local hats = game.ReplicatedStorage.Hats

script.Parent.MouseButton1Click:Connect(function()
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	
	if humanoid then
		humanoid:AddAccessory(hats.BloxxerCap:Clone())
	end
end)
1 Like

I did try do some more digging while waiting for a response, I forgot to add that I was doing this in a local script and just found out that you cant add accessories to humanoid in a local script so i fixed it by using a remote event. Thanks for replying though!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.