Error with hat changing system

Hello, I am trying to make a system that changes the player’s hat once they reach a certain amount of points. The problem comes when I run it and reach the needed points, it does nothing and doesn’t give me an error in the output. I can’t find anything that is wrong with the script so I am not sure what is causing the error to happen.

local Players = game:GetService("Players")

local function giveHat(plr)
	local Humanoid = plr.Character:WaitForChild("Humanoid")
	local shaggy = Instance.new("Accessory")
	shaggy.Name = "Shaggy"

	local handle = Instance.new("Part")
	handle.Name = "Handle"
	handle.Size = Vector3.new(1,1.6,1)
	handle.Parent = shaggy

	local faceFrontAttachment = Instance.new("Attachment")
	faceFrontAttachment.Name = "HatAttachment"
	faceFrontAttachment.Position = Vector3.new(0.12, 0.4, -0.07)
	faceFrontAttachment.Parent = handle

	local mesh = Instance.new("SpecialMesh")
	mesh.Name = "Mesh"
	mesh.Scale = Vector3.new(1.07, 1.07, 1.07)
	mesh.MeshId = "rbxassetid://19999424"
	mesh.TextureId = "rbxassetid://20571982"
	mesh.Parent = handle

	Humanoid:AddAccessory(shaggy)
end

local function checkPoints(plr, value)
if plr.leaderstats.Robux.Value >=25 then 
	game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
		checkPoints(plr,plr.leaderstats.Robux.Value)
	end)

Players.PlayerAdded:Connect(function(plr)
	plr.leaderstats.Robux.Changed:Connect(function()
		checkPoints(plr, plr.leaderstats.Robux)
	end)
1 Like

I’m unsure if this is the issue, but can you try setting the accessory’s parent to workspace? And if that doesn’t work, can you ensure that the accessory is being actually created?

Adding on to what Neptunial said

`shaggy.Parent = plr.Character.Head

You should load the asset into the player’s head, here’s an example:

local function giveHat(plr)
    local accessory = game:GetService("InsertService"):LoadAsset(417457139):GetChildren()[1]|
    accessory.Parent = plr.Character
    accessory.Name = "Shaggy"
end

Let me know if it helps! :grinning_face_with_smiling_eyes: