Hat won't go on character

I’m trying to get a player to wear a space helmet when they interact with a prompt, but it’s spawning where the hat already is (on the mannequin), and it isn’t removing the character’s hats, why?

local player = game:GetService("Players").LocalPlayer
local SpaceShip = workspace:WaitForChild("SpaceShipTest")
local SpaceSuit = workspace.SpaceSuit

local AstronautHelm = game.Workspace.SpaceSuit.AstronautHelmet

local function GiveAccessory(player)
	local character = player.Character
	local humanoid = character.Humanoid
	for _, v in humanoid:GetChildren() do
		if v:IsA("Accessory") then
			if v.Handle:FindFirstChild("HatAttachment") then
				v:Destroy()
			end
		end
	end
	local accessory = AstronautHelm:Clone()
	accessory.Handle.Anchored = false
	humanoid:AddAccessory(accessory)
end



SpaceSuit.HumanoidRootPart.ProximityPrompt.Triggered:Connect(GiveAccessory)

The code provided works for me. (Ran in a server script.)


I see a lot of inconsistencies in the code.

  1. You can’t get Player in a Server Script.
  2. game.Workspace.SpaceSuit when there’s already a variable for SpaceSuit.
  3. The code you provided might be running on a LocalScript, you cannot detect .Triggered