I created this script for a game where you can try on various rich outfits by clicking on a button. Everything works except for one thing, and that is that the clothing (which is in the script) is not applied to the bundle that the player is wearing. I have already looked at the error box but no error messages were displayed there.
Does anyone know what I did wrong?
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local assetService = game:GetService("AssetService")
local changeEvent = replicatedStorage:WaitForChild("ChangeClothesAndAccessories")
local newShirtId = 17455580758
local newPantsId = 172750508
local newHairId = 9726877283 -- Replace with the actual hair asset ID
local newAccessories = {
96079043,
4506963722,
6682339691,
6414371617,
13398965847,
17758492892,
10875654340,
15133946399,
6134532324,
12872970789,
-- Add more accessory IDs as needed
}
local newBundleId = 239
-- Function to remove old clothes and accessories
local function removeOldClothesAndAccessories(character)
-- Remove old clothes
for _, child in pairs(character:GetChildren()) do
if child:IsA("Shirt") or child:IsA("Pants") then
child:Destroy()
end
end
-- Remove old accessories
for _, child in pairs(character:GetChildren()) do
if child:IsA("Accessory") then
child:Destroy()
end
end
-- Remove old tools
for _, tool in pairs(character:GetChildren()) do
if tool:IsA("Tool") then
tool:Destroy()
end
end
-- Remove old hair
for _, child in pairs(character:GetChildren()) do
if child:IsA("Accessory") and child.AccessoryType == Enum.AccessoryType.Hair then
child:Destroy()
end
end
end
-- Function to add new clothes and accessories
local function addNewClothesAndAccessories(character, player)
-- Add new shirt
local newShirt = Instance.new("Shirt", character)
newShirt.ShirtTemplate = "rbxassetid://" .. newShirtId
-- Add new pants
local newPants = Instance.new("Pants", character)
newPants.PantsTemplate = "rbxassetid://" .. newPantsId
-- Add new hair
local success, hairModel = pcall(function()
return game:GetService("InsertService"):LoadAsset(newHairId)
end)
if success then
local hair = hairModel:FindFirstChildOfClass("Accessory")
if hair then
hair.Parent = character
end
hairModel:Destroy()
else
warn("Failed to load hair with ID: " .. tostring(newHairId))
end
-- Add new accessories
for _, accessoryId in ipairs(newAccessories) do
local success, accessoryModel = pcall(function()
return game:GetService("InsertService"):LoadAsset(accessoryId)
end)
if success then
local accessory = accessoryModel:FindFirstChildOfClass("Accessory")
if accessory then
accessory.Parent = character
end
accessoryModel:Destroy()
else
warn("Failed to load accessory with ID: " .. tostring(accessoryId))
end
end
-- Add new tools from ServerStorage
local vipToolsFolder = serverStorage:FindFirstChild("VIPTools")
if vipToolsFolder then
for _, tool in pairs(vipToolsFolder:GetChildren()) do
if tool:IsA("Tool") then
local clonedTool = tool:Clone()
clonedTool.Parent = player.Backpack
end
end
else
warn("VIPTools folder not found in ServerStorage")
end
end
-- Function to apply new bundle
local function applyNewBundle(humanoid, bundleId)
if bundleId then
humanoid.RequiresNeck = false
local function tryGet(context, object, funcName, ...)
local success, result = pcall(object[funcName], object, ...)
if success then
return result
else
warn("Invalid", context .. ':', ..., "(Error:", result .. ')')
end
end
local bundle
local info = tryGet("BundleId", assetService, "GetBundleDetailsAsync", bundleId)
if info then
local id = 0
for _, item in pairs(info.Items) do
if item.Type == "UserOutfit" then
id = item.Id
break
end
end
if id > 0 then
bundle = tryGet("Bundle", players, "GetHumanoidDescriptionFromOutfitId", id)
end
end
if bundle then
humanoid:ApplyDescription(bundle)
end
end
end
-- Function to handle the RemoteEvent
local function onChangeClothesAndAccessories(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
applyNewBundle(humanoid, newBundleId)
removeOldClothesAndAccessories(character)
addNewClothesAndAccessories(character, player)
-- Make parts invisible
local partsToHide = {"RightFoot", "RightLowerLeg", "RightUpperLeg", "Head"}
for _, partName in ipairs(partsToHide) do
local part = character:FindFirstChild(partName)
if part then
part.Transparency = 1
end
end
-- Remove face
local face = character.Head:FindFirstChildOfClass("Decal")
if face then
face:Destroy()
end
end
end
changeEvent.OnServerEvent:Connect(onChangeClothesAndAccessories)
I have now only specified the script for the ServerScriptService so the other is not relevant (because everything works except that the shirt and pants are applied to the bundle)