Trouble Detecting Clicking Player

I want to make a script that takes the character’s accessories and puts them in server storage. The problem is that it won’t detect the player that is clicking the button.

LocalScript:

script.Parent.MouseButton1Click:Connect(function(Player)
	local Char = Player.Character or Player.CharacterAdded:Wait()
	local X = Char:GetChildren()
	for i = 1, #X do
		if X[i].ClassName == "Accessory" then
			X[i].Parent = game.ServerStorage
		end
	end
end)

It keeps saying the Player parameter is nil.

MouseButton1Click doesn’t have a Player parameter passed over. Instead, if it is a local script, you can get the player by doing game.Players.LocalPlayer
Another issue is the LocalScript cannot access ServerStorage, hence the “Server” part. An alternative to ServerStorage would be ReplicatedStorage, but it depends on your context. Also, I’d like to direct you to a nice thing with humanoids, you can do Humanoid:GetAccessories() to get all the accessories of the character as well. Something that might work is:

script.Parent.MouseButton1Click:Connect(function()
	local Char = game.Players.LocalPlayer.Character
	local X = Char.Humanoid:GetAccessories()
	for i = 1, #X do
		X[i].Parent = game.ReplicatedStorage
	end
end)

It works, but how do I differentiate between whos accessories are whos? Also, how do I get rid of them after the player dies?

We can simply make a folder and put the accessories in those. Name the folder after the player and it is way more organized.

local folder = Instance.new('Folder')
folder.Name = game.Players.LocalPlayer.Name
folder.Parent = game.ReplicatedStorage
script.Parent.MouseButton1Click:Connect(function()
	local Char = game.Players.LocalPlayer.Character
	local X = Char.Humanoid:GetAccessories()
	for i = 1, #X do
		X[i].Parent = folder
	end
end)

edit: I didnt see the 2nd part of your post, to do that simply add a connection to humanoid.Died, and when its called for loop through the folder and delete all the accessories.

As @Soliform mentioned, a folder is how you would tell which accessories belong to who. And in terms of your 2nd question, if you want to remove the accessories, just do what he also stated, here’s how you would do that

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local folder = Instance.new('Folder')
folder.Name = plr.Name
folder.Parent = game.ReplicatedStorage

script.Parent.MouseButton1Click:Connect(function()
	local X = humanoid:GetAccessories()
	for i = 1, #X do
		X[i].Parent = folder
	end
end)

humanoid.Died:Connect(function()
    for i,v in pairs(folder:GetChildren()) do
       v:Destroy()
    end
end)

This should hopefully work, I moved the character declaration outside of that event to allow for the died event to work.

Edit: Saw things in my example code that could’ve been done better

With your script, it allows two folder to be in Replicated Storage. I made where instead of destroying the children, it destroyed the entire folder.

I’m guessing this script is located in StarterGui? If it’s the case then yea you need to destroy the folder instead since when the palyer dies, any scripts in startergui happen again because they get copied to the playergui, but glad you managed to figure out that issue yourself!

Also I’m trying to make it where the player can get their accessories back, but I am having the same problem with the player parameter. This is a Script instead of a LocalScript.

Script:

local Storage = game.ReplicatedStorage

script.Parent.MouseButton1Click:Connect(function(player)
	local Char = player.Character or player.CharacterAdded:Wait()
	local Folder = Storage:FindFirstChild(player)
	local A = Folder:GetChildren()
	for i = 1, A do
		if A[i].ClassName == "Accessory" then
			A[i].Parent = game.Workspace:FindFirstChild(player)
		end
	end
end)

Is this because it is in the StarterGui?

Again, the issue is is that there’s no player paramter in MouseButton1Click, you have to somehow get the player yourself, which I don’t think is possible from a script in this case, you can add code to the one you have in localscript to do what you want

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local buttonpressed = false

local folder = Instance.new('Folder')
folder.Name = plr.Name
folder.Parent = game.ReplicatedStorage

script.Parent.MouseButton1Click:Connect(function()
    if not buttonpressed then -- Accessories still on
		local X = humanoid:GetAccessories()
		for i = 1, #X do
			X[i].Parent = folder
		end
	else --Accessories in folder
		for i,v in pairs(folder:GetChildren()) do
			v.Parent = char
		end
	end
	buttonpressed = not buttonpressed
end)

humanoid.Died:Connect(function()
    for i,v in pairs(folder:GetChildren()) do
       v:Destroy()
    end
end)

This should work hopefully

I had to do some changing up, but this is the final working script:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local Revert = script.Parent.Parent.Revert

local folder = Instance.new('Folder')
folder.Name = plr.Name
folder.Parent = game.ReplicatedStorage

script.Parent.MouseButton1Click:Connect(function()
	local X = char.Humanoid:GetAccessories()
	for i = 1, #X do
		X[i].Parent = folder
	end
end)

Revert.MouseButton1Click:Connect(function()
	for i,v in pairs(folder:GetChildren()) do
		v.Parent = char
	end
end)

humanoid.Died:Connect(function()
	folder:Destroy()
end)

Alright, glad it’s working for you now! If you have anymore issues don’t be afraid to make another post!