Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. local DataStore = game:GetService("DataStoreService"):GetDataStore("CharacterApparel")
  2. local Appearances = {}
  3. local SwapHair = game:GetService("ReplicatedStorage").HairTypeEvent
  4. local SwapHairColor = game:GetService("ReplicatedStorage").HairColorEvent
  5.  
  6. local HairColors = {"Really black", "Crimson", "Forest green", "Dark indigo", "New Yeller", "Institutional white", "Deep orange", "Reddish brown"} -- etc
  7. local Hairs = {"BlackHair", "ChoppyHair", "Afro", "FancyHair"} -- etc
  8. local Face = {"Smile", "Angry", "Sad", "Joyful"} -- etc
  9. local Scars = {"Light", "Medium", "Heavy"} -- etc
  10.  
  11. local DefaultApparel = {
  12. Hair = "BlackHair",
  13. Face = "Smile",
  14. Apparel = "SurvivalClothes",
  15. BodyBuild = "Normal",
  16.  
  17. HairColor = "Really black",
  18. BodyColor = "Nougat",
  19.  
  20. DidGetSaved = "No"
  21. }
  22.  
  23. local function DeepCopy(tab)
  24. if type(tab) ~= "table" then return tab end
  25. local newtab = {}
  26. for key, val in pairs(tab) do
  27. newtab[DeepCopy(key)] = DeepCopy(val)
  28. end
  29. return newtab
  30. end
  31. function Repeat(func, times)
  32. local success, ret
  33. local time = 0
  34. repeat time = 1 success, ret = pcall(func)
  35. until success or time >= times
  36. return success, ret
  37. end
  38. function Load(UserId)
  39. local success, data = Repeat(function() return DataStore:GetAsync(UserId) end, 3)
  40. if success then
  41. if data ~= nil then
  42. if type(data) == "string" then if not pcall(function() data = game:GetService("HttpService"):JSONDecode(data) end) then return end
  43. for key, val in pairs(data) do
  44. Appearances[UserId][key] = val -- Loads data into the appearance, but keeps if you add more things
  45. end
  46. end
  47. else
  48. error("Couldn't access DataStoreService!") -- Crashes the script so people won't have progress loss (or notify them that you couldn't load their data
  49. end
  50. end
  51. end
  52. function Save(UserId)
  53. local success = Repeat(function() DataStore:SetAsync(UserId, Appearances[UserId]) end, 3)
  54. if not success then
  55. error("Couldn't access DataStoreService!") -- Or notify the player that you didn't save their data...
  56. end
  57. end
  58.  
  59. game.Players.PlayerAdded:Connect(function(Player)
  60. Appearances[Player.userId] = DeepCopy(DefaultApparel)
  61. Load(Player.userId)
  62. Player.CharacterAdded:connect(function(Character) -- so if player respawns, we want to add the hair too... (and other things like coloring, you also need to do in the changing event and there
  63. local Appearance = Appearances[Player.userId]
  64. Player:LoadCharacterAppearance(script.Parent.HairFolder[Appearance.Hair]:Clone()) --Hair
  65. local CurrentHair = Player.Character:findFirstChild(Appearance.Hair) --HairColor
  66. if CurrentHair then CurrentHair.Handle.BrickColor = BrickColor.new(Appearance.HairColor)end -- HairColor
  67. end)
  68. end)
  69.  
  70. game.Players.PlayerRemoving:Connect(function(Player)
  71. Save(Player.userId)
  72. Appearances[Player.userId] = nil
  73. end)
  74.  
  75. function FindInTable(tab, val)
  76. for key, value in pairs(tab) do if value == val then return key end end
  77. return nil
  78. end
  79.  
  80. SwapHairColor.OnServerEvent:connect(function(Player)
  81. local Appearance = Appearances[Player.userId]
  82. print(Appearance.HairColor)
  83. Appearance.HairColor = FindInTable(HairColors, Appearance.HairColor) + 1 > #HairColors and HairColors[1] or HairColors[FindInTable(HairColors, Appearance.HairColor) + 1] -- Finds in table current color and selects next. If next doesn't exist, it chooses first instead
  84. local HairsToBeColored = script.Parent.HairFolder:GetChildren()
  85. print(HairsToBeColored.Name)
  86. for i, v in pairs(HairsToBeColored) do
  87. v.Handle.BrickColor = BrickColor.new(Appearance.HairColor)
  88. end
  89. local CurrentHair = Player.Character:findFirstChild(Appearance.Hair)
  90. if CurrentHair then CurrentHair.Handle.BrickColor = BrickColor.new(Appearance.HairColor)end
  91. end)
  92.  
  93. SwapHair.OnServerEvent:connect(function(Player)
  94. local Appearance = Appearances[Player.userId]
  95. print(Appearance.Hair)
  96. local OldHair = Player.Character:FindFirstChild(Appearance.Hair)
  97. if OldHair then OldHair:Destroy() end -- before we change Appearance.Hair
  98. Appearance.Hair = FindInTable(Hairs, Appearance.Hair) + 1 > #Hairs and Hairs[1] or Hairs[FindInTable(Hairs, Appearance.Hair) + 1] -- Finds in table current color and selects next. If next doesn't exist, it chooses first instead
  99. Player:LoadCharacterAppearance(script.Parent.HairFolder[Appearance.Hair]:Clone())
  100. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement