Hello, recently i have made a script that when you click a button it clones a hat from replicated storage to players head but im having a problem when it was supposed to attach players head. Someone pls help me solve this. It spawns mid air in the middle of the map
script:
local button = script.Parent
local hatName = script.Parent.Parent.Name
local player = game.Players.LocalPlayer
button.MouseButton1Click:Connect(function()
local hat = game.ReplicatedStorage:WaitForChild(“Hats”):FindFirstChild(hatName)
if hat then
local clonedHat = hat:Clone()
local character = player.Character or player.CharacterAdded:Wait()
for _, accessory in pairs(character:GetChildren()) do
if accessory:IsA("Accessory") and accessory.Name == hatName then
accessory:Destroy()
end
end
clonedHat.Parent = character
end