Button doesn’t equip hat when pressed

I have a local script inside of a text button, and here’s the script i used:

script.Parent.MouseButton1Click:Connect(function(Player)
local clone = game.ServerStorage.RainbowHalo:Clone()
clone.Parent = Player.Character
end)

Whenever you click the button, you get this error:
RainbowHalo is not a valid member of ServerStorage “ServerStorage”
The halo is an accessory in server storage. I’m not completely sure what’s happening.

There are two things that cause the error to occur.

MouseButton1Click doesn’t have a parameter. So you can define the player with LocalPlayer, which is only valid for local scripts.

local Player = game:GetService("Players").LocalPlayer

Clients have no access to ServerStorage. You may move it to ReplicatedStorage, where the things are replicated between both server and client.

local clone = game:GetService("ReplicatedStorage").RainbowHalo

And also, make sure to use the GetService method when getting services. Guaranteed.

local Player = game:GetService("Players").LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    local clone = game:GetService("ReplicatedStorage").RainbowHalo:Clone()
    clone.Parent = Player.Character
end)

Use game.ServerStorage:WaitForChild("RainbowHalo") as the reason could be that the item does NOT get a chance to load.

So I would just put the accessory in replicated storage, correct?

Of course. ServerStorage can be seen by servers only.

I’ll also do this too, in a mix with the replicated storage.

I’m getting what’s basically the same error, but with replicatedstorage.
RainbowHalo is not a valid member of ReplicatedStorage “ReplicatedStorage”

Can you show us your Explorer please?

Where specifically inside my explorer, Replicated storage or the Gui?

ReplicatedStorage______________________________

Also did you take @exp_lol123’s advice?Try using :WaitForChild() function.

Sorry for bad quality, had to take it from my phone

Does autofill display it while you’re trying to type it?

Yes___________________________.

When you press play, is the hat still there?

Yes____________________________.

I edited the script.

Just forgot to add Clone function. This may be why it printed the error again because if you hit second time, it would not be added.

Try it again.

Maybe use Humanoid:AddAccessory() instead of directly parenting the hat to the character.

This is what I’m trying right now, but not much success.
local Player = game:GetService(“Players”).LocalPlayer

script.Parent.MouseButton1Click:Connect(function()

local clone = game:GetService(“ReplicatedStorage”)

local halo = clone:WaitForChild(“RainbowHalo”):Clone()

halo.Parent = Player.Character

end)

I dont think I’ve ever used AddAccessory, how do you use it?