So basically I want to achieve a character swap system where the appearance (hair, accessories, shirts, and pants) changes as you press the number keys. The UIS works perfectly and does a good job on changing the shirts and pants. My main issue is that hair and other accessories do not appear with the rest yet the code still works perfectly.
I have tried everything from :AddAccessories to :Clone then .Parent
My current code (In StarterCharacterScripts):
local plr = game.Players.LocalPlayer
local chr = plr.Character or script.Parent
local UserInput = game:GetService("UserInputService")
local repsto = game:GetService("ReplicatedStorage")
local acornShirt = repsto.Wardrobe.acornShirt
local acornPants = repsto.Wardrobe.acornPants
local acornHair = repsto.Wardrobe.aaronHair
local baconShirt = repsto.Wardrobe.baconShirt
local baconPants = repsto.Wardrobe.baconPants
local baconHair = repsto.Wardrobe.baconHair
local baconAccessory = repsto.Wardrobe.baconAccessory
local blueShirt = repsto.Wardrobe.blueShirt
local bluePants = repsto.Wardrobe.bluePants
local blueHair = repsto.Wardrobe.blueHair
local coryShirt = repsto.Wardrobe.coryShirt
local coryPants = repsto.Wardrobe.coryPants
local coryHair = repsto.Wardrobe.coryHair
local guestShirt = repsto.Wardrobe.guestShirt
local guestPants = repsto.Wardrobe.guestPants
local guestHat = repsto.Wardrobe.guestHat
local manShirt = repsto.Wardrobe.manShirt
local manPants = repsto.Wardrobe.manPants
local manHat = repsto.Wardrobe.manHat
local aaronShirt = repsto.Wardrobe.aaronShirt
local aaronPants = repsto.Wardrobe.aaronPants
local aaronHair = repsto.Wardrobe.aaronHair
local DB = true
UserInput.InputBegan:Connect(function(switch)
if switch.KeyCode == Enum.KeyCode.One then
if DB == true then
DB = false
for i, v in pairs(chr:GetDescendants()) do
if v:IsA("Accessory") then
v:Destroy()
end
end
chr.Shirt.ShirtTemplate = acornShirt.ShirtTemplate
chr.Pants.PantsTemplate = acornPants.PantsTemplate
chr.Head.BrickColor = BrickColor.new("Nougat")
local acornhairclone = acornHair:Clone()
acornhairclone.Parent = chr
--- I have no idea why when i press 1 it only copies aaronhair
--- and the hair always spawns on 0, 0, 0 then falls
wait(0.5)
DB = true
end
end
if switch.KeyCode == Enum.KeyCode.Two then
if DB == true then
DB = false
for i, v in pairs(chr:GetDescendants()) do
if v:IsA("Accessory") then
v:Destroy()
end
end
chr.Shirt.ShirtTemplate = baconShirt.ShirtTemplate
chr.Pants.PantsTemplate = baconPants.PantsTemplate
chr.Head.BrickColor = BrickColor.new("Pastel brown")
local baconhairclone = baconHair:Clone()
baconhairclone.Parent = chr
--- same case here
wait(0.5)
DB = true
end
end
end)
The clothes work and you can alternate between the two. My only concern is why no hair?
After some searchs in the docs I found that Humanoid:AddAccessory() does not work as you expect on the client side because the accessory weld created on the server side and when you use it on the client it will just add the accessory inside of the character without welding it, to solve this you can use remote events to tell the server to add the accessories when the player press the keys, or if you wanna add the accessory on the client side, you can use this addAccoutrementr function made by TheGamer101 in this topic like this:
local plr = game.Players.LocalPlayer
local chr = plr.Character or script.Parent
local UserInput = game:GetService("UserInputService")
local repsto = game:GetService("ReplicatedStorage")
local acornShirt = repsto.Wardrobe.acornShirt
local acornPants = repsto.Wardrobe.acornPants
local acornHair = repsto.Wardrobe.aaronHair
local baconShirt = repsto.Wardrobe.baconShirt
local baconPants = repsto.Wardrobe.baconPants
local baconHair = repsto.Wardrobe.baconHair
local baconAccessory = repsto.Wardrobe.baconAccessory
local blueShirt = repsto.Wardrobe.blueShirt
local bluePants = repsto.Wardrobe.bluePants
local blueHair = repsto.Wardrobe.blueHair
local coryShirt = repsto.Wardrobe.coryShirt
local coryPants = repsto.Wardrobe.coryPants
local coryHair = repsto.Wardrobe.coryHair
local guestShirt = repsto.Wardrobe.guestShirt
local guestPants = repsto.Wardrobe.guestPants
local guestHat = repsto.Wardrobe.guestHat
local manShirt = repsto.Wardrobe.manShirt
local manPants = repsto.Wardrobe.manPants
local manHat = repsto.Wardrobe.manHat
local aaronShirt = repsto.Wardrobe.aaronShirt
local aaronPants = repsto.Wardrobe.aaronPants
local aaronHair = repsto.Wardrobe.aaronHair
local DB = true
function weldAttachments(attach1, attach2)
local weld = Instance.new("Weld")
weld.Part0 = attach1.Parent
weld.Part1 = attach2.Parent
weld.C0 = attach1.CFrame
weld.C1 = attach2.CFrame
weld.Parent = attach1.Parent
return weld
end
local function buildWeld(weldName, parent, part0, part1, c0, c1)
local weld = Instance.new("Weld")
weld.Name = weldName
weld.Part0 = part0
weld.Part1 = part1
weld.C0 = c0
weld.C1 = c1
weld.Parent = parent
return weld
end
local function findFirstMatchingAttachment(model, name)
for _, child in pairs(model:GetChildren()) do
if child:IsA("Attachment") and child.Name == name then
return child
elseif not child:IsA("Accoutrement") and not child:IsA("Tool") then -- Don't look in hats or tools in the character
local foundAttachment = findFirstMatchingAttachment(child, name)
if foundAttachment then
return foundAttachment
end
end
end
end
function addAccoutrement(character, accoutrement)
accoutrement.Parent = character
local handle = accoutrement:FindFirstChild("Handle")
if handle then
local accoutrementAttachment = handle:FindFirstChildOfClass("Attachment")
if accoutrementAttachment then
local characterAttachment = findFirstMatchingAttachment(character, accoutrementAttachment.Name)
if characterAttachment then
weldAttachments(characterAttachment, accoutrementAttachment)
end
else
local head = character:FindFirstChild("Head")
if head then
local attachmentCFrame = CFrame.new(0, 0.5, 0)
local hatCFrame = accoutrement.AttachmentPoint
buildWeld("HeadWeld", head, head, handle, attachmentCFrame, hatCFrame)
end
end
end
end
UserInput.InputBegan:Connect(function(switch)
if switch.KeyCode == Enum.KeyCode.One then
if DB == true then
DB = false
for i, v in pairs(chr:GetDescendants()) do
if v:IsA("Accessory") then
v:Destroy()
end
end
chr.Shirt.ShirtTemplate = acornShirt.ShirtTemplate
chr.Pants.PantsTemplate = acornPants.PantsTemplate
chr.Head.BrickColor = BrickColor.new("Nougat")
local acornhairclone = acornHair:Clone()
addAccoutrement(chr, acornhairclone)
--- I have no idea why when i press 1 it only copies aaronhair
--- and the hair always spawns on 0, 0, 0 then falls
--- it's because the hair is not welded to the head so it will just fall.
wait(0.5)
DB = true
end
end
if switch.KeyCode == Enum.KeyCode.Two then
if DB == true then
DB = false
for i, v in pairs(chr:GetDescendants()) do
if v:IsA("Accessory") then
v:Destroy()
end
end
chr.Shirt.ShirtTemplate = baconShirt.ShirtTemplate
chr.Pants.PantsTemplate = baconPants.PantsTemplate
chr.Head.BrickColor = BrickColor.new("Pastel brown")
local baconhairclone = baconHair:Clone()
addAccoutrement(chr, baconhairclone)
wait(0.5)
DB = true
end
end
end)