Hello, I’ve been trying to solve this for hours, but I couldn’t figure it out, so I’m posting here. (I’m using a google translator.)
There is an R6 rig in the workspace. I’m working on a script where the rig changes to the player’s character when the player enters their username in a GUI textbox.
local Players = game:GetService("Players")
local TextBox = script.Parent.Parent.MainFrame.AnimationScrollingFrame.PlayerNameTextBox
-- Dummy rig reference in workspace
local MainRig = workspace:WaitForChild("MainRig")
-- Create StringValue for UserId if it doesn't exist
local userIdValue = MainRig:FindFirstChild("UserId")
if not userIdValue then
userIdValue = Instance.new("StringValue")
userIdValue.Name = "UserId"
userIdValue.Parent = MainRig
end
-- Function to reset the model's appearance
local function resetModel()
for _, item in pairs(MainRig:GetChildren()) do
if item:IsA('CharacterMesh') or item:IsA('Shirt') or
item:IsA('Pants') or item:IsA('Accessory') or
item:IsA('ShirtGraphic') then
item:Destroy()
end
end
if MainRig.Head:FindFirstChild('Mesh') then
MainRig.Head.Mesh:Destroy()
end
end
-- Function to update the model's appearance
local function updateModel(userId)
local success, error = pcall(function()
local appearance = Players:GetCharacterAppearanceAsync(userId)
for _, item in pairs(appearance:GetChildren()) do
if item:IsA('SpecialMesh') or item:IsA('BlockMesh') or item:IsA('CylinderMesh') then
item.Parent = MainRig.Head
elseif item:IsA('Decal') then
MainRig.Head.face.Texture = item.Texture
elseif item:IsA('BodyColors') or item:IsA('CharacterMesh') or
item:IsA('Shirt') or item:IsA('Pants') or item:IsA('ShirtGraphic') then
if MainRig:FindFirstChild('Body Colors') then
MainRig['Body Colors']:Destroy()
end
item.Parent = MainRig
elseif item:IsA('Accessory') then
item.Parent = MainRig
item.Handle.CFrame = MainRig.Head.CFrame * CFrame.new(0, MainRig.Head.Size.Y / 2, 0) *
item.AttachmentPoint:inverse()
end
end
if not MainRig.Head:FindFirstChild('Mesh') then
local mesh = Instance.new('SpecialMesh', MainRig.Head)
mesh.MeshType = Enum.MeshType.Head
mesh.Scale = Vector3.new(1.25, 1.25, 1.25)
end
end)
if not success then
warn("Failed to update appearance:", error)
end
end
-- Function to handle the username input
local function onUsernameSubmitted(username)
local success, userId = pcall(function()
return Players:GetUserIdFromNameAsync(username)
end)
if success and userId then
resetModel()
userIdValue.Value = userId
updateModel(userId)
else
warn("Failed to get UserId for username:", username)
end
end
-- Connect the TextBox's FocusLost event
TextBox.FocusLost:Connect(function(enterPressed)
if enterPressed then
local username = TextBox.Text
if username ~= "" then
onUsernameSubmitted(username)
end
end
end)
I wrote the code as described above, but some accessories were not loaded in the correct position and were not properly anchored. Also, the code looks a bit complicated—would there be a simpler way to achieve this?
Thank you for reading!