You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve?
I want to achieve a basic avatar custom NPC/Player Maker based on IDs
- What is the issue?
my issue is this code I’ve made its not loading the hairs including that I want to basically load one through 3 hats.
- What solutions have you tried so far?
Since its basically inserting items with InsertService and AssetService I don’t think it will be for me since considering I’m kind of stupid. Help is appreciated.
Main Code:
local AssetService = game:GetService("AssetService")
local MarketplaceService = game:GetService("MarketplaceService")
local InsertService = game:GetService("InsertService")
--// Module Functions
local isBodyPart = require(script:WaitForChild("isBodyPart"))
--// Modules
local Generator = require(script:WaitForChild("Generator"))
local Util = require(script:WaitForChild("Util"))
--// Types
export type AvatarParam = {
["Character"]: Model?,
["IsR15"]: boolean?,
["UseOldBodyParts"]: boolean?,
["UseOldBodyParts"]: boolean?,
["Genders"]: {
["Male"]: {
["Bundles"]: {number},
["Accessories"]: {
["BackAccessory"]: {number},
["FaceAccessory"]: {number},
["FrontAccessory"]: {number},
["HairAccessory"]: {number},
["HatAccessory"]: {number},
["NeckAccessory"]: {number},
["ShouldersAccessory"]: {number},
["WaistAccessory"]: {number},
},
["Heads"]: {number},
["Faces"]: {number},
["Colors"]: {
{Color3}
}
},
["Female"]: {
["Bundles"]: {number},
["Accessories"]: {number},
["Heads"]: {number},
["Colors"]: {
{Color3}
}
}
}
}
local Avatar = {}
Avatar.AvatarParam = {}
--// Functions
--// Function to fetch body parts from a bundle ID
local function getBodyPartsFromBundle(bundleID)
local success, bundleDetails = pcall(function()
return AssetService:GetBundleDetailsAsync(bundleID)
end)
if success and bundleDetails then
-- Filter out the body parts
local bodyParts = {}
for _, item in pairs(bundleDetails.Items) do
local info = MarketplaceService:GetProductInfo(item.Id)
if isBodyPart(info.AssetTypeId) then
table.insert(bodyParts, item)
end
end
return bodyParts
else
warn("Failed to get bundle details for ID: " .. tostring(bundleID))
return nil
end
end
--// A Function to see what the instance its talking about checking if the the item is in the Instance so it can return it but if it doesn't then it' keep on checking
local function findItemInInstance(whereIsItIn: Instance, className)
for _, v in ipairs(whereIsItIn:GetChildren()) do
if v:IsA(className) then
return v
else
continue
end
end
end
--// Avatar Param
function Avatar.AvatarParam.new(): AvatarParam
return {} :: AvatarParam
end
--// Avatar Main Function
function Avatar:LoadAvatar(Param: AvatarParam)
--// Variables
local Params = Param
local IsR15 = if Params.IsR15 == nil then true else Params.IsR15
local UseOldBodyParts = Params.UseOldBodyParts or false
local Character = Params.Character or script.DefaultCharacters:FindFirstChild(IsR15 and "R15" or "R6"):Clone()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Genders = Params.Genders
local indexGender = Random.new():NextInteger(1,2) and "Male" or "Female"
local Gender = Genders["Male"]
local Bundles = Gender["Bundles"]
local indexBundle = Random.new():NextInteger(1,#Bundles)
local Bundle = Bundles[indexBundle]
local BundleItems = getBodyPartsFromBundle(Bundle)
local Heads = Gender["Heads"]
local indexHead = Random.new():NextInteger(1,#Heads)
local Head = Heads[indexHead]
local Accessories = Gender["Accessories"]
local Faces = Gender["Faces"]
local indexFace = Random.new():NextInteger(1,#Faces)
local Face = Faces[indexFace]
--// Inserting a Folder
local BodyParts = Instance.new("Folder", script); BodyParts.Name = "BodyParts"
--// Putting all the thing in the BundleItems into the "BodyParts" Folder Class
for _, v in ipairs(BundleItems) do
for _, i in ipairs(InsertService:LoadAsset(v.Id):GetChildren()) do
i:Clone().Parent = BodyParts
end
end
--// Getting the bundle
for _, v in ipairs(BodyParts:GetChildren()) do
if Humanoid.RigType == Enum.HumanoidRigType.R15 or IsR15 then
if v.Name == "R15ArtistIntent" and not UseOldBodyParts then
local inInstance = v:GetChildren()
for i=1, #inInstance do
local BodyPart = inInstance[i]
Humanoid:ReplaceBodyPartR15(Enum.BodyPartR15[BodyPart.Name], BodyPart)
end
elseif v.Name == "R15Fixed" and UseOldBodyParts then
local inInstance = v:GetChildren()
for i=1, #inInstance do
local BodyPart = inInstance[i]
Humanoid:ReplaceBodyPartR15(Enum.BodyPartR15[BodyPart.Name], BodyPart)
end
end
else
if v.Name == "R6" then
local inInstance = v:GetChildren()
for i=1, #inInstance do
local BodyPart = inInstance[i]
BodyPart.Parent = Character
end
end
end
end
--// Load the head Asset from the random of heads and parent it to the script
InsertService:LoadAsset(Head):GetChildren()[1].Parent = script
--// Find the instance that is in the script
local item = findItemInInstance(script, "SpecialMesh")
if item then
--// Don't warn the User since the item will be placed on the Players/NPCs Head
item.Parent = Character:FindFirstChild("Head")
else
--// Warn the user that the item is not gaurented found
warn('I have not found the item')
end
for n, v in pairs(Accessories) do
local amount = math.random(0,1)
if n == "HairAccessory" then
amount = 1
end
local assetId = Util:ArrayToCommaSeperatedList(Generator:Random(v, amount))
if assetId ~= 0 then
local assetId = tonumber(assetId)
if assetId == nil then
break
end
InsertService:LoadAsset(assetId):GetChildren()[1].Parent = Character
end
end
--local item = findItemInInstance(script, "Accessory")
--if item then
-- --// Don't warn the User since the item will be placed on the Players/NPCs Character
-- item.Parent = Character
--else
-- --// Warn the user that the item is not gaurented found
-- warn('I have not found the item')
--end
--// Check if the Face is ID 0 so it can use the textured one
if Face == 0 then
--// If its 0 then use the default face since it can't be imported
local DFace = script.DefaultFace:WaitForChild("face")
Character:FindFirstChild("Head"):FindFirstChildOfClass("Decal").Texture = DFace.Texture
else
InsertService:LoadAsset(Face):GetChildren()[1].Parent = script
local item = findItemInInstance(script, "Decal")
if item then
--// Don't warn the User since the item's texture will be placed on the Players/NPCs Face
Character:FindFirstChild("Head"):FindFirstChildOfClass("Decal"):Destroy()
item.Parent = Character:FindFirstChild("Head")
else
--// Warn the user that the item is not gaurented found
warn('I have not found the item')
end
end
--// Destroying the BodyParts Folder
BodyParts:Destroy()
return Character
end
return Avatar