Applying layered clothing to NPC via client

As funny as it is: I script in single lines, I didn’t want to waste much time to indent it since I was also exploding in joy I had found the solution.

If you want I can indent the code and apply proper comments to help people out. (Sorry for the delay on the answer, I don’t really check the forums consistently)

I actually just felt obligated to do that now, so here it is:

--Place this in a LocalScript inside of ReplicatedFirst.
repeat wait() until game:IsLoaded()
--^ Simple code just to wait the game finish loading.
local pl,sc,wm,vw,vwc=game.Players.LocalPlayer,Instance.new("ScreenGui"),Instance.new("WorldModel"),Instance.new("ViewportFrame"),Instance.new("Camera")
--^ This line of code isn't that relevant, it is there to create the necessary GUI elements just to showcase the solution and set up some other variables.
sc.Parent = pl:WaitForChild("PlayerGui")
vw.Size = UDim2.new(0.5,0,0.5,0)
vw.CurrentCamera = vwc
vw.Parent = sc
wm.Parent = vw
vwc.Parent = vw
--^ This small section basically sets up a test GUI, a viewport and a camera for the test.
local ch = pl.Character or pl.CharacterAdded:Wait()
repeat wait() until (ch:FindFirstChildWhichIsA("Humanoid",true) and ch:FindFirstChildWhichIsA("HumanoidDescription",true))
--^ These two lines of code waits for your character to spawn, puts it in a variable and checks if the game already properly loaded it in.
--Once it does that it basically grabs it's humanoid description to build a rig for the test. (Requiring you to actually be wearing a layered clothing for this test!)
local vwm = game.Players:CreateHumanoidModelFromDescription(ch:FindFirstChildWhichIsA("HumanoidDescription",true),ch:FindFirstChildWhichIsA("Humanoid").RigType)
--^ Rig creating line of code. (Yeah.)
--VVV SOLUTION VVV
game:GetService("ContentProvider"):PreloadAsync({vwm})
--^^^ SOLUTION ^^^
--This line of code gets the ContentProvider service and preloads the model.
--By preloading the rig's model, it basically loads all data assigned to it in your client even before being placed into the world.
--I don't know what is the cause of the issue, but it may have something to do with how cage-meshes are dealt with, maybe (Take the next sentence with a grain of salt.) they require to be ran in the physics simulation to load in properly, in which a GUI lacks that.
--And so (Also take this with a grain of salt.) preloading probably runs it through the physics simulation, allowing it to load-in properly.
--DISCLAIMER THAT THIS IS AN ASSUMPTION LITERALLY MADE BY SOMEONE WHO IS *NOT* A ROBLOX ENGINEER. (Third time I'll say this, take those sentences with a grain of salt.)
vwm:SetPrimaryPartCFrame(CFrame.new(0,vwm:FindFirstChildWhichIsA("Humanoid").HipHeight+vwm.PrimaryPart.Size.Y/2,0))
--^ Sets the rig's position in the viewmodel in a way it's legs doesn't stick through the floor.
vwm.Name = "ViewmodelRig"
vwm.Parent = wm
vwc.CFrame = CFrame.new(Vector3.new(0,5,-5),vwm.PrimaryPart.Position+Vector3.new(0,1,0))
--^ Places the rig into the viewport and sets up the camera via a very simple CFrame operation.
4 Likes