Advertisement
Waffle3z

avatarchange

Aug 14th, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. local defaultR15 = game:GetService("InsertService"):LoadAsset(978469541)
  2. local defaultR6 = game:GetService("InsertService"):LoadAsset(978511844)
  3.  
  4. local cache = {}
  5. local function ChangeAvatar(player, id)
  6.     if not (id > 0) or id%1 ~= 0 then id = 1 end
  7.     local character = player.Character
  8.     if not character or not character:FindFirstChild("Humanoid") then return end
  9.     local assets = cache[id] or game.Players:GetCharacterAppearanceAsync(id)
  10.     player:ClearCharacterAppearance()
  11.    
  12.     local R15 = character.Humanoid.RigType == Enum.HumanoidRigType.R15
  13.     local default = (R15 and defaultR15 or defaultR6):Clone()
  14.     for _, part in pairs(default:GetChildren()) do
  15.         local old = character:FindFirstChild(part.Name)
  16.         part.Parent = character
  17.         if old then
  18.             part.CFrame = old.CFrame
  19.             old:Destroy()
  20.         end
  21.     end
  22.     local humanoid = character.Humanoid
  23.     humanoid:BuildRigFromAttachments()
  24.     humanoid.Parent = nil
  25.     humanoid.Parent = character
  26.    
  27.     local content = Instance.new("Folder")
  28.     for _, v in pairs(assets:GetChildren()) do
  29.         v:Clone().Parent = content
  30.         if (R15 and v.Name == "R15") or (not R15 and v.Name == "R6") then
  31.             if R15 then
  32.                 for _, part in pairs(v:GetChildren()) do
  33.                     local old = character:FindFirstChild(part.Name)
  34.                     part:Clone().Parent = character
  35.                     old:Destroy()
  36.                 end
  37.             else
  38.                 for _, CharacterMesh in pairs(v:GetChildren()) do
  39.                     CharacterMesh.Parent = character
  40.                 end
  41.             end
  42.         elseif (R15 and v.Name ~= "R6") or (not R15 and v.Name ~= "R15") then
  43.             if v:IsA("FaceInstance") then
  44.                 character.Head.face.Texture = v.Texture
  45.             elseif v:IsA("DataModelMesh") then
  46.                 character.Head.Mesh:Destroy()
  47.                 v.Parent = character.Head
  48.             else
  49.                 spawn(function()
  50.                     pcall(function()
  51.                         player:LoadCharacterAppearance(v)
  52.                     end)
  53.                 end)
  54.             end
  55.         end
  56.     end
  57.     cache[id] = content
  58.     humanoid:BuildRigFromAttachments()
  59. end
  60.  
  61. local function AddPlayer(player)
  62.     player.Chatted:Connect(function(msg)
  63.         if msg:sub(1, 7):lower() == "avatar/" then
  64.             local name = msg:sub(8)
  65.             local id = game.Players:GetUserIdFromNameAsync(name)
  66.             if id then
  67.                 ChangeAvatar(player, id)
  68.             end
  69.         elseif msg:sub(1, 9):lower() == "avatarid/" then
  70.             local id = tonumber(msg:sub(10))
  71.             if id then
  72.                 ChangeAvatar(player, id)
  73.             end
  74.         end
  75.     end)
  76. end
  77.  
  78. game.Players.PlayerAdded:Connect(AddPlayer)
  79. for _, player in pairs(game.Players:GetPlayers()) do
  80.     AddPlayer(player)
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement