Hat giver shop not adding hat but deducting currency?

local event = game.ReplicatedStorage.HatPurchased

event.OnServerEvent:Connect(function(plr,hat,price)
	local vibez = plr.leaderstats:FindFirstChild("Vibez")
	
	if vibez then
		if vibez.Value >= price then
			plr.CharacterAdded:Connect(function(char)
				local hatobj = game.ServerStorage.Hats[hat]:Clone()
				char.Humanoid:AddAccessory(hatobj)

			end)
			vibez.Value = vibez.Value - price
		end
	end
end)

My server script ^

script.Parent.MouseButton1Click:Connect(function(plr)
	local remote = game.ReplicatedStorage.HatPurchased
	remote:FireServer("FrozenFireHorns",10)
end)

The local script in the button that I pressed ^^

For some reason it deducts the currency, but never ends up adding them to my avatar? Did I do something wrong?

It has no errors.

The reason it doesn’t add it is because it’ll only add it once the character respawns, you have to put the code for it outside as well so it will also work the first time it is bought

local event = game.ReplicatedStorage.HatPurchased

event.OnServerEvent:Connect(function(plr,hat,price)
	local vibez = plr.leaderstats:FindFirstChild("Vibez")
	
	
	if vibez then
		if vibez.Value >= price then
			local hatobj = game.ServerStorage.Hats[hat]:Clone()
			plr.Character.Humanoid:AddAccessory(hatobj)
			plr.CharacterAdded:Connect(function(char)
				local hatobj = game.ServerStorage.Hats[hat]:Clone()
				char.Humanoid:AddAccessory(hatobj)
			end)
			vibez.Value = vibez.Value - price
		end
	end
end)

This should hopefully work as it’ll add the hat first time and then when the character respawns

You need an attachment on the handle that it then can attach on its head attachment I think

When using :AddAccessory you don’t need to position it, it automatically places it on like your default hats put on your avatar when you join a game.

Yes but you need an attachment in the handle that has the same name of an attachment on the character so name it HatAttachment if you want it on the hat

The script I got already works, without attachments.

Did you check the Developer API page where it literally tells you what you need?

Also not sure if you’re aware, but you shouldn’t be getting the price from the client’s arguments. An exploiter could easily buy any hat and just say the price is 0.

It’s probably better to have a shared ModuleScript in ReplicatedStorage where both the client and server can check the price.

Well it works for me so I’ma just stay with it lol. Topic done.