Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DataStore = game:GetService("DataStoreService"):GetDataStore("CharacterApparel")
- local Appearances = {}
- local SwapHair = game:GetService("ReplicatedStorage").HairTypeEvent
- local SwapHairColor = game:GetService("ReplicatedStorage").HairColorEvent
- local HairColors = {"Really black", "Crimson", "Forest green", "Dark indigo", "New Yeller", "Institutional white", "Deep orange", "Reddish brown"} -- etc
- local Hairs = {"BlackHair", "ChoppyHair", "Afro", "FancyHair"} -- etc
- local Face = {"Smile", "Angry", "Sad", "Joyful"} -- etc
- local Scars = {"Light", "Medium", "Heavy"} -- etc
- local DefaultApparel = {
- Hair = "BlackHair",
- Face = "Smile",
- Apparel = "SurvivalClothes",
- BodyBuild = "Normal",
- HairColor = "Really black",
- BodyColor = "Nougat",
- DidGetSaved = "No"
- }
- local function DeepCopy(tab)
- if type(tab) ~= "table" then return tab end
- local newtab = {}
- for key, val in pairs(tab) do
- newtab[DeepCopy(key)] = DeepCopy(val)
- end
- return newtab
- end
- function Repeat(func, times)
- local success, ret
- local time = 0
- repeat time = 1 success, ret = pcall(func)
- until success or time >= times
- return success, ret
- end
- function Load(UserId)
- local success, data = Repeat(function() return DataStore:GetAsync(UserId) end, 3)
- if success then
- if data ~= nil then
- if type(data) == "string" then if not pcall(function() data = game:GetService("HttpService"):JSONDecode(data) end) then return end
- for key, val in pairs(data) do
- Appearances[UserId][key] = val -- Loads data into the appearance, but keeps if you add more things
- end
- end
- else
- 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
- end
- end
- end
- function Save(UserId)
- local success = Repeat(function() DataStore:SetAsync(UserId, Appearances[UserId]) end, 3)
- if not success then
- error("Couldn't access DataStoreService!") -- Or notify the player that you didn't save their data...
- end
- end
- game.Players.PlayerAdded:Connect(function(Player)
- Appearances[Player.userId] = DeepCopy(DefaultApparel)
- Load(Player.userId)
- 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
- local Appearance = Appearances[Player.userId]
- Player:LoadCharacterAppearance(script.Parent.HairFolder[Appearance.Hair]:Clone()) --Hair
- local CurrentHair = Player.Character:findFirstChild(Appearance.Hair) --HairColor
- if CurrentHair then CurrentHair.Handle.BrickColor = BrickColor.new(Appearance.HairColor)end -- HairColor
- end)
- end)
- game.Players.PlayerRemoving:Connect(function(Player)
- Save(Player.userId)
- Appearances[Player.userId] = nil
- end)
- function FindInTable(tab, val)
- for key, value in pairs(tab) do if value == val then return key end end
- return nil
- end
- SwapHairColor.OnServerEvent:connect(function(Player)
- local Appearance = Appearances[Player.userId]
- print(Appearance.HairColor)
- 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
- local HairsToBeColored = script.Parent.HairFolder:GetChildren()
- print(HairsToBeColored.Name)
- for i, v in pairs(HairsToBeColored) do
- v.Handle.BrickColor = BrickColor.new(Appearance.HairColor)
- end
- local CurrentHair = Player.Character:findFirstChild(Appearance.Hair)
- if CurrentHair then CurrentHair.Handle.BrickColor = BrickColor.new(Appearance.HairColor)end
- end)
- SwapHair.OnServerEvent:connect(function(Player)
- local Appearance = Appearances[Player.userId]
- print(Appearance.Hair)
- local OldHair = Player.Character:FindFirstChild(Appearance.Hair)
- if OldHair then OldHair:Destroy() end -- before we change Appearance.Hair
- 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
- Player:LoadCharacterAppearance(script.Parent.HairFolder[Appearance.Hair]:Clone())
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement