Add accessories to your character by clicking button (+Shirt, pants change?)

I’m going to be very blunt. I know diddly nor squat about coding, and I have zero clue why this code would or would not work. Or any code for that matter.

What I want: To click a button and it applies 4 accessories, and changes the shirt and pants. I have a working click-to-change-clothes script, but I don’t know if I can just throw the accessory script on top of it or if the code has to be in one script? Right now the scripts are in separate blocks for testing purposes but i will eventually want them all in one.

What happens: I click the block and nothing happens

Here’s my Frankensteined attempt to code a click-to-give-accessories

script.Parent.ClickDetector.MouseClick:Connect(function(player) 
	local hatClone = game.ReplicatedStorage.Hat:Hat()
	hatClone.Parent = game.Workspace:FindFirstChild(player.Name)
	local shoulderClone = game.ReplicatedStorage.Shoulder:Shoulder1()
	shoulderClone.Parent = game.Workspace:FindFirstChild(player.Name)
	local shoulderClone = game.ReplicatedStorage.Shoulder:Shoulder2()
	shoulderClone.Parent = game.Workspace:FindFirstChild(player.Name)
	local frontClone = game.ReplicatedStorage.Front:Front()
	frontClone.Parent = game.Workspace:FindFirstChild(player.Name)
	
end)

Iheaveaheadache

There is most likely a much easier way to make this code, I honestly thought as long as I have the shirt/accessory IDs it would just be as simple as putting them into the code : |

Note I do not want to make a morph with the shirt and accessories, I just want the items applied to the player’s character, and if that means the clothes don’t apply (if they have rthro) or their current accessories stick through the ones I apply, so be it.

Also, if needed, here’s the click-to-change-clothes script that DOES work

script.Parent.ClickDetector.MouseClick:Connect(function(Player)
	local Shirt1 = script.Parent:WaitForChild("Shirt"):Clone()
	local Pants1 = script.Parent:WaitForChild("Pants"):Clone()
	if (Player) and (Player.Character) then
		for _, Item in pairs(Player.Character:GetChildren()) do
			if Item:IsA("Shirt") then
				Item:Destroy()
				Shirt1.Parent = Player.Character
			elseif Item:IsA("Pants") then
				Item:Destroy()
			end
		end
	end
	Shirt1.Parent = Player.Character
	Pants1.Parent = Player.Character
end)

image
And here’s what’s in that part.
Do I need to combine these scripts or will adding two scripts to one part make it do both?

I apologize if I’m not giving enough information, I am extremely confused by all this and have been trying to make this work for a week.

1 Like

So the issue here is that you aren’t cloning them.

script.Parent.ClickDetector.MouseClick:Connect(function(player) 
	local hatClone = game.ReplicatedStorage.Hat:Clone()
	hatClone.Parent = game.Workspace:FindFirstChild(player.Name)
	local shoulderClone = game.ReplicatedStorage.Shoulder:Clone()
	shoulderClone.Parent = game.Workspace:FindFirstChild(player.Name)
	local shoulderClone = game.ReplicatedStorage.Shoulder:Clone()
	shoulderClone.Parent = game.Workspace:FindFirstChild(player.Name)
	local frontClone = game.ReplicatedStorage.Front:Clone()
	frontClone.Parent = game.Workspace:FindFirstChild(player.Name)
end)

Also you could use Humanoid:AddAccessory() or use HumanoidDescription. Just adding :Clone() should fix your code though.

2 Likes

So, I applied the code, and it didn’t work. I figured out how to open the dev console and it said something about replicated storage being incorrect and I looked at the code again and adjusted some of the names (Shoulder(s), Hat to 1 & 2 respectively and Hat1) and it still didn’t work.

I looked to the dev console again to figure out what was wrong and… nothing, it gave 0 error. I have no clue what I’m doing wrong or how to fix it. ><

Sorry for the late reply, but here’s some much cleaner code (added comments so you can understand what it does):

--> Services ( Good practice to use GetService )
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--> Name of accessories in ReplicatedStorage ( You can add more )
local Accessories: {string} = {"Hat", "Front", "Shoulder1", "Shoulder2"}

--> function  ( ran when the ClickDetector is clicked )
local function AddAccessories(player: Player)
   local Character = player.Character -- Get Character
   local Humanoid: Humanoid = Character:FindFirstChildWhichIsA("Humanoid") -- Get Humanoid
   if not Humanoid then -- If Humanoid couldn't be found then warn server
      return warn(string.format("%s Humanoid couldn't be located", player.Name))
   end

   --> Loop through the accessory name and checks to see if they exist in ReplicatedStorage
   for _,  AccessoryName: string in Accessories do
      local Accessory = ReplicatedStorage:FindFirstChild(AccessoryName)
      if not Accessory then
         continue
      end
 
      --> If Accessory Exists then add accessory to humanoid
      Humanoid:AddAccessory(Accessory:Clone())
   end
end

--> Mouse Click Connection
script.Parent.ClickDetector.MouseClick:Connect(AddAccessories)

Ideally, I would have a folder inside of replicated storage and then you can loop through that, though this should work with your current layout. Hope this helps!

1 Like