Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 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. local function Respawned(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. if Player.Character then Respawned(Player.Character) end -- checking does character already exist (before binding the event @down) - it makes actually sense because the character is spawned before the event so no sense to call the event after
  69. Player.CharacterAdded:Connect(Respawned)
  70. end)
  71.  
  72. game.Players.PlayerRemoving:Connect(function(Player)
  73. Save(Player.userId)
  74. Appearances[Player.userId] = nil
  75. end)
  76.  
  77. function FindInTable(tab, val)
  78. for key, value in pairs(tab) do if value == val then return key end end
  79. return nil
  80. end
  81.  
  82. SwapHairColor.OnServerEvent:connect(function(Player)
  83. local Appearance = Appearances[Player.userId]
  84. 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
  85. local HairsToBeColored = script.Parent.HairFolder:GetChildren()
  86. print(HairsToBeColored.Name)
  87. for i, v in pairs(HairsToBeColored) do
  88. v.Handle.BrickColor = BrickColor.new(Appearance.HairColor)
  89. end
  90. local CurrentHair = Player.Character:findFirstChild(Appearance.Hair)
  91. if CurrentHair then CurrentHair.Handle.BrickColor = BrickColor.new(Appearance.HairColor)end
  92. print(Appearance.HairColor)
  93. end)
  94.  
  95. SwapHair.OnServerEvent:connect(function(Player)
  96. local Appearance = Appearances[Player.userId]
  97. local OldHair = Player.Character:FindFirstChild(Appearance.Hair)
  98. if OldHair then OldHair:Destroy() end -- before we change Appearance.Hair
  99. 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
  100. Player:LoadCharacterAppearance(script.Parent.HairFolder[Appearance.Hair]:Clone())
  101. print(Appearance.Hair)
  102. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement