Layered Clothing On NPCs

Hello everyone! I’ve been working on a project and had issues implementing 3D clothing. After a few hours, I found the solution. When I looked on the Dev Forum I found very little on npcs with layered clothing, so I decided to make this to help out others with the same issue. This is my first ever tutorial so please bare with me :sweat_smile:

Step 1: Set Up Remote Events and GUI
This step is very simple. Create a simple GUI with a Text (or Image) Button and a Remote Event inside Replicated Storage.
image
image

Step 2: The Local Script
Insert a local script into your button.
Then you need to define your Remote Event and Button (if you want)

local RemoteEvent = game.ReplicatedStorage.ApplyClothing
local Button = script.Parent

Then run a function when the button is pressed.

Button.MouseButton1Click:Connect(function()
	
	RemoteEvent:FireServer()
	
end)

That’s it for the button, very simple.

Step 3: Applying Humanoid Description to NPC
Firstly, you need to create a Script inside Server Script Service
Then you need to create variables for your remote event and the npc you wish to apply the HumanDesc to.

local Event = game.ReplicatedStorage.ApplyClothing
local Dummy = workspace.Dummy

After that you need to run a function when the Event is fired. For the function’s parameters you’ll need to get the player.

Event.OnServerEvent:Connect(function(player)
--our code will go here
end)

Now, we’ll get started on the main functions of this tutorial. I’ll explain the code and then show all of it below.

First the code gets a HumanoidDescription of the player and saves it for later use. After that, the code iterates through the player’s character. It then looks for a player’s body part (a mesh part) and then looks for a WrapTarget inside of that body part. If it is found, it will copy it and then move it to the NPC. If there is already a WrapTarget inside the NPC, it will delete it and replace it with the newer copy. If the code cant find the NPC’s body part then the copy gets deleted. After all the wrap targets are copied and moved to the NPC, the Humanoid Description is applied and the clothing will wrap around the NPC’s body properly.

The final code for the Server Script

local Event = game.ReplicatedStorage.ApplyClothing
local Dummy = workspace.Dummy

Event.OnServerEvent:Connect(function(player) --runs function when fired 
	
	local HumanoidDesc = game.Players:GetHumanoidDescriptionFromUserId(player.UserId) --gets the player's appearance
	
	for i,v in ipairs(player.Character:GetChildren()) do
		
		if v:IsA("MeshPart") then --looks for the character's body parts
			
			if v:FindFirstChildWhichIsA("WrapTarget") then --looks for a wrap target inside the body
				
				local WT = v:FindFirstChildWhichIsA("WrapTarget"):Clone() --if it is found it will clone it WT is just an abbreviation
				local TargetBodyPart = Dummy[WT.Name] --it then sets the target body part; the wrap's name is always the same as the body part its parented to, so example LeftUpperLeg WT will look for the dummy's LeftUpperLeg
				
				if TargetBodyPart:IsA("MeshPart") then --if the body part is a mesh; some heads on npcs aren't meshes which Wraps cant be parented to
					
					if TargetBodyPart:FindFirstChildWhichIsA("WrapTarget") then  TargetBodyPart:FindFirstChildWhichIsA("WrapTarget"):Destroy() end --checks for any pre-existing WTs and then deletes them
					WT.Parent = Dummy[WT.Name] --puts the newer clone into the body part
					
				else
					
					WT:Destroy() --clone is deleted if body part cant be found or is not a mesh
					
				end
			end
		end
	end
	
	Dummy.Humanoid:ApplyDescription(HumanoidDesc) --applies the description, and wraps the clothing properly
end)

I really hoped this helps out! I didn’t find any posts on this but if someone did this before me lmk so I can credit them. Feedback is appreciated.

Here is a link to an uncopylocked game that has a demo of this tutorial: 3D Clothing On NPCs Demo - Roblox

6 Likes

Nice article, but instead of using Players:GetHumanoidDescriptionFromUserId(), you could try using Humanoid:GetAppliedDescription(). It works on players and doesn’t do an unnecessary API call which could fail.

3 Likes

bruh why is your avatar like that LOL

4 Likes

I tried to put on as much 3D clothing as I could to make sure every type of 3D clothing works lol

1 Like