Advertisement
Guest User

My Data Store

a guest
Nov 13th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.42 KB | None | 0 0
  1. local PlayerDataService = {}
  2.  
  3. local DataStores2 = require(script.DataStore2)
  4.  
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6. local Players               = game:GetService("Players")
  7. local RunService            = game:GetService("RunService")
  8.  
  9. local PlayerCollection      = {}
  10. local userDataStoreName = "TestingDataStore1"
  11.  
  12. local DefaultData = {
  13.     Name            = "N/A",
  14.     UserId          = 0,
  15.  
  16.     HP = {                      -- Formula: [BASE HP] + ( ( [LEVEL] * [LEVEL MULTIPLIER] ) + ( [HP POINTS] * [HP POINT MULTIPLIER] ) )
  17.         Base        = 1000,
  18.         Multiplier = {
  19.             Level   = 40,
  20.             Point   = 53
  21.         }
  22.     },
  23.  
  24.     Stamina = {                 --  [BASE STAMINA] + ( ( [LEVEL] * [LEVEL MULTIPLIER] ) + ( [STAMINA POINTS] * [POINT MULTIPLIER] ) )
  25.         Base        = 100,
  26.         Current     = 100,
  27.         Max         = 100,
  28.         Multiplier = {
  29.             Level   = 5,
  30.             Point   = 5
  31.         }
  32.     },
  33.  
  34.     Level = {
  35.         Current         = 1,
  36.         Prestige        = 0,
  37.         EXP         = 0,   
  38.         Multiplier  = 100, 
  39.         Max = 300, 
  40.     },
  41.  
  42.     RedeemedCodes = {},
  43.  
  44.     ["Stats"] = {
  45.         Health  = 0,        -- HP
  46.         Strength    = 0,        -- Damage
  47.         Dexterity   = 0,        -- Accuracy + Crit Rate
  48.         Quirk   = 0,        -- Accuracy + Crit Rate
  49.         Stamina = 0,        -- Stamina
  50.         Points  = 3
  51.     },
  52.  
  53.     Traits = {
  54.         Selected    = 128911039142763,
  55.         TraitStorage    = {}
  56.     },
  57.  
  58.     MoveSlots = {  
  59.         Slot1 = "",
  60.         Slot2 = "",
  61.         Slot3 = "",
  62.         Slot4 = "",
  63.         Slot5 = "",
  64.         Slot6 = "",
  65.         Slot7 = "",
  66.         Slot8 = "",
  67.     },
  68.  
  69.     CustomChar = { 
  70.  
  71.         Hair1 = "Hair1",
  72.         Hair1Color = "0 0 0",
  73.         Hair2 = "Hair1",
  74.         Hair2Color = "0 0 0",
  75.         Shirt = 2774526319,
  76.         Pants = 4763486305,
  77.         Face = 0,
  78.         SkinColor = "1 0.86 0.52",
  79.         Customized = false
  80.     },
  81.  
  82.     Quirk = {
  83.         Ability     = "Quirkless",
  84.         Stores = {},
  85.         Multiplier  = 1000,
  86.         Max = 100
  87.  
  88.     },
  89.  
  90.     Yen             = 0,
  91.  
  92.     Spins =  {
  93.         Common      = 3,
  94.         Uncommon    = 0,
  95.         Rare            = 0,
  96.         PlusUltra       = 0
  97.     },
  98.  
  99.     Faction         = "Civilian",
  100.     Fame            = 0,
  101.  
  102.     GymTime     = 0,
  103.     ExpTime         = 0,
  104.     YenTime         = 0,
  105.  
  106.     Arena = {
  107.         Wins        = 0,
  108.         Loses       = 0,
  109.         TotalTime   = 0,
  110.         ABSW        = 0,
  111.     },
  112.  
  113.     Inventory = {}
  114. }
  115.  
  116. function StringToColor(String)
  117.  
  118.     local command = (String):split(" ")
  119.  
  120.     local R = nil
  121.     local G = nil
  122.     local B = nil
  123.  
  124.     for i, v in pairs(command) do
  125.  
  126.         if i == 1 then
  127.  
  128.             R = tonumber(v)
  129.  
  130.         elseif i == 2 then
  131.  
  132.             G = tonumber(v)
  133.  
  134.         elseif i == 3 then
  135.  
  136.             B = tonumber(v)
  137.  
  138.         end
  139.     end
  140.  
  141.     return Color3.new(R,G,B)
  142.  
  143. end
  144.  
  145. function DeepTransfer(orig, targeted)
  146.  
  147.     targeted["Level"]["Max"] = orig["Level"]["Max"]
  148.  
  149.     for i, child in pairs(orig) do
  150.  
  151.         local child_type = type(child)
  152.  
  153.         if child_type == 'table' then
  154.  
  155.             for v, child2 in pairs(child) do
  156.  
  157.                 local child2_type = type(child2)
  158.  
  159.                 if child2_type == 'table' then
  160.  
  161.                     for h, child3 in pairs(child2) do
  162.  
  163.                         if targeted[i][v][h] == nil then
  164.                             targeted[i][v][h] = child3
  165.                         end
  166.  
  167.                     end
  168.  
  169.                 else
  170.  
  171.                     if targeted[i][v] == nil then
  172.                         targeted[i][v] = child2
  173.                     end
  174.  
  175.                 end
  176.             end
  177.  
  178.         else   
  179.  
  180.             if targeted[i] == nil then
  181.                 targeted[i] = child
  182.             end
  183.  
  184.         end
  185.     end
  186. end
  187.  
  188.  
  189.  
  190. function DeepCopy(orig, copies)
  191.     copies = copies or {}
  192.     local orig_type = type(orig)
  193.     local copy
  194.     if orig_type == 'table' then
  195.         if copies[orig] then
  196.             copy = copies[orig]
  197.         else
  198.             copy = {}
  199.             copies[orig] = copy
  200.             for orig_key, orig_value in next, orig, nil do
  201.                 copy[DeepCopy(orig_key, copies)] = DeepCopy(orig_value, copies)
  202.             end
  203.             setmetatable(copy, DeepCopy(getmetatable(orig), copies))
  204.         end
  205.     else -- number, string, boolean, etc
  206.         copy = orig
  207.     end
  208.     return copy
  209. end
  210.  
  211.  
  212.  
  213. function PlayerDataService.ManualSave(Player)
  214.  
  215.     local PlayerId = Player.UserId
  216.  
  217.     if PlayerCollection[PlayerId] ~= nil then
  218.  
  219.         local PlayerData = PlayerCollection[PlayerId]
  220.  
  221.         DataStores2(userDataStoreName, Player):Set(PlayerData)
  222.         PlayerDataService.UpdateInstances(Player)
  223.  
  224.     end
  225.  
  226. end
  227.  
  228. function PlayerDataService.new(playerid)
  229.  
  230.     if PlayerCollection[playerid] then
  231.         return PlayerCollection[playerid]
  232.     end
  233.  
  234.     local PlayerData = DeepCopy(DefaultData)
  235.  
  236.     PlayerCollection[playerid] = PlayerData
  237.  
  238.     local Player = game.Players:GetPlayerByUserId(playerid)
  239.  
  240.  
  241.     local userData = DataStores2(userDataStoreName, Player):Get(PlayerData)
  242.  
  243.     coroutine.resume(coroutine.create(function()
  244.  
  245.         while wait(10) do
  246.  
  247.             DataStores2(userDataStoreName, Player):Set(userData)
  248.  
  249.         end
  250.  
  251.     end))
  252.  
  253.  
  254.  
  255.     coroutine.resume(coroutine.create(function()
  256.  
  257.         game.Players.PlayerRemoving:Connect(function(plr)
  258.  
  259.             if plr.Name == Player.Name then
  260.  
  261.                 DataStores2(userDataStoreName, Player):Set(userData)
  262.  
  263.             end
  264.  
  265.         end)
  266.  
  267.     end))  
  268.  
  269.     DeepTransfer(PlayerData, userData)
  270.  
  271.     PlayerCollection[playerid] = userData
  272.  
  273.     coroutine.resume(coroutine.create(function()   
  274.  
  275.         if userData.CustomChar.Customized == false then
  276.             game.ReplicatedStorage.DataSystem.TpToCustomization:FireClient(Player)
  277.         end
  278.  
  279.     end))  
  280.  
  281.     return userData
  282.  
  283. end
  284.  
  285. function UpdateInstance(InstanceData,path, key, value)
  286.  
  287.     local _instance = nil
  288.     if path then
  289.         _instance = InstanceData:FindFirstChild(path)
  290.         if _instance then
  291.             _instance = _instance:FindFirstChild(key)
  292.         end
  293.     else
  294.         _instance = InstanceData:FindFirstChild(key)
  295.     end
  296.  
  297.     if _instance then
  298.         _instance.Value = value
  299.     end
  300.  
  301. end
  302.  
  303.  
  304. function PlayerDataService.UpdateInstances(Player)
  305.  
  306.     local PlayerData = PlayerDataService.new(Player.UserId)
  307.     local InstanceData = Player:WaitForChild("InstanceData")
  308.  
  309.     if PlayerData.Quirk["Stores"] == nil then
  310.         PlayerData.Quirk["Stores"] = {}
  311.     end
  312.  
  313.     if PlayerData.Quirk.Stores[PlayerData.Quirk.Ability] == nil then
  314.         PlayerData.Quirk.Stores[PlayerData.Quirk.Ability] = {}
  315.     end
  316.  
  317.     if PlayerData.Quirk.Stores[PlayerData.Quirk.Ability]["Level"] == nil then
  318.         PlayerData.Quirk.Stores[PlayerData.Quirk.Ability]["Level"] = 1
  319.     end
  320.  
  321.     if PlayerData.Quirk.Stores[PlayerData.Quirk.Ability]["Exp"] == nil then
  322.         PlayerData.Quirk.Stores[PlayerData.Quirk.Ability]["Exp"] = 0
  323.     end
  324.  
  325.     UpdateInstance(InstanceData,"Level", "Current",         PlayerData.Level.Current)
  326.     UpdateInstance(InstanceData,"Level", "EXP",             math.floor(PlayerData.Level.EXP))
  327.     UpdateInstance(InstanceData,"Level", "RequiredEXP",     math.floor(PlayerData.Level.Current * PlayerData.Level.Multiplier))
  328.  
  329.     UpdateInstance(InstanceData,"Stats", "Health",          PlayerData.Stats.Health)
  330.     UpdateInstance(InstanceData,"Stats", "Points",          PlayerData.Stats.Points)
  331.     UpdateInstance(InstanceData,"Stats", "Stamina",     PlayerData.Stats.Stamina)
  332.     UpdateInstance(InstanceData,"Stats", "Strength",        PlayerData.Stats.Strength)
  333.     UpdateInstance(InstanceData,"Stats", "Quirk",       PlayerData.Stats.Quirk)
  334.  
  335.     UpdateInstance(InstanceData,"Quirk", "Ability",         PlayerData.Quirk.Ability)
  336.     UpdateInstance(InstanceData,"Quirk", "Level",           PlayerData.Quirk.Stores[PlayerData.Quirk.Ability]["Level"])
  337.     UpdateInstance(InstanceData,"Quirk", "EXP",             PlayerData.Quirk.Stores[PlayerData.Quirk.Ability]["Exp"])
  338.     UpdateInstance(InstanceData,nil, "MaxHealth",           PlayerData.HP.Base + ( ( PlayerData.Level.Current * PlayerData.HP.Multiplier.Level ) + ( PlayerData.Stats.Health * PlayerData.HP.Multiplier.Point ) ) )
  339.  
  340.     UpdateInstance(InstanceData,"Stamina", "Max",       PlayerData.Stamina.Base + ((PlayerData.Level.Current * PlayerData.Stamina.Multiplier.Level) + (PlayerData.Stats.Stamina * PlayerData.Stamina.Multiplier.Point)))
  341.     UpdateInstance(InstanceData,"Stamina", "Current",       PlayerData.Stamina.Current)
  342.     UpdateInstance(InstanceData,"Stats", "Points",          PlayerData.Stats.Points)
  343.     UpdateInstance(InstanceData,nil, "Yen",                 PlayerData.Yen)
  344.  
  345.     UpdateInstance(InstanceData,"Spins", "Common",      PlayerData.Spins.Common)
  346.     UpdateInstance(InstanceData,"Spins", "Uncommon",    PlayerData.Spins.Uncommon)
  347.     UpdateInstance(InstanceData,"Spins", "Rare",            PlayerData.Spins.Rare)
  348.     UpdateInstance(InstanceData,"Spins", "PlusUltra",       PlayerData.Spins.PlusUltra)
  349.     UpdateInstance(InstanceData,nil, "EXPTime",         PlayerData.ExpTime)
  350.     UpdateInstance(InstanceData,nil, "YenTime",         PlayerData.YenTime)
  351.     Player:WaitForChild("leaderstats"):WaitForChild("Fame").Value = PlayerData.Fame
  352.  
  353. end
  354.  
  355. --if RunService:IsServer() then
  356. game.Players.PlayerAdded:Connect(function(Player)
  357.     Player.CharacterAdded:Connect(function(Character)
  358.  
  359.         coroutine.resume(coroutine.create(function()
  360.  
  361.             local PlayerData = PlayerDataService.new(Player.UserId)
  362.  
  363.             PlayerDataService.UpdateInstances(Player)
  364.  
  365.             Character.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=" .. PlayerData.CustomChar.Shirt
  366.             Character.Pants.PantsTemplate = "http://www.roblox.com/asset/?id=" .. PlayerData.CustomChar.Pants
  367.             Character.Head.Face.Texture = "http://www.roblox.com/asset/?id=" .. PlayerData.CustomChar.Face  
  368.  
  369.             local SkinColor = PlayerData.CustomChar.SkinColor
  370.  
  371.             Character.Skin.HeadColor3 = StringToColor(SkinColor)
  372.  
  373.             Character.Skin.LeftArmColor3 = StringToColor(SkinColor)
  374.             Character.Skin.RightArmColor3 = StringToColor(SkinColor)
  375.             Character.Skin.RightLegColor3 = StringToColor(SkinColor)
  376.             Character.Skin.LeftLegColor3 = StringToColor(SkinColor)
  377.             Character.Skin.TorsoColor3 = StringToColor(SkinColor)
  378.  
  379.             local Hair1 = game.ReplicatedStorage.ModuleScript.AllAsssets.Hairs:FindFirstChild(PlayerData.CustomChar.Hair1):Clone()
  380.             Hair1.Parent = Character       
  381.             local Hair1Color = PlayerData.CustomChar.Hair1Color    
  382.             Hair1.Handle.Color = StringToColor(Hair1Color)
  383.  
  384.             local Hair2 = game.ReplicatedStorage.ModuleScript.AllAsssets.Hairs:FindFirstChild(PlayerData.CustomChar.Hair2):Clone()
  385.             Hair2.Parent = Character               
  386.             local Hair2Color = PlayerData.CustomChar.Hair2Color        
  387.             Hair2.Handle.Color = StringToColor(Hair2Color)
  388.  
  389.         end))  
  390.  
  391.         if not Player:FindFirstChild("InstanceData") then
  392.             local InstanceData = ReplicatedStorage:WaitForChild("Stats"):WaitForChild("InstanceData"):Clone()
  393.             InstanceData.Parent = Player
  394.  
  395.             local PlayerData = PlayerDataService.new(Player.UserId)
  396.  
  397.             UpdateInstance(InstanceData,"Level", "Current",         PlayerData.Level.Current)
  398.             UpdateInstance(InstanceData,"Level", "EXP",             math.floor(PlayerData.Level.EXP))
  399.             UpdateInstance(InstanceData,"Level", "RequiredEXP",     math.floor(PlayerData.Level.Current * PlayerData.Level.Multiplier))
  400.  
  401.             UpdateInstance(InstanceData,"Stats", "Health",          PlayerData.Stats.Health)
  402.             UpdateInstance(InstanceData,"Stats", "Points",          PlayerData.Stats.Points)
  403.             UpdateInstance(InstanceData,"Stats", "Stamina",     PlayerData.Stats.Stamina)
  404.             UpdateInstance(InstanceData,"Stats", "Strength",        PlayerData.Stats.Strength)
  405.             UpdateInstance(InstanceData,"Stats", "Quirk",       PlayerData.Stats.Quirk)
  406.  
  407.             UpdateInstance(InstanceData,"Quirk", "Ability",         PlayerData.Quirk.Ability)
  408.             UpdateInstance(InstanceData,"Quirk", "Level",           PlayerData.Quirk.Level)
  409.             UpdateInstance(InstanceData,"Quirk", "EXP",             PlayerData.Quirk.EXP)
  410.             UpdateInstance(InstanceData,nil, "MaxHealth",           PlayerData.HP.Base + ( ( PlayerData.Level.Current *     PlayerData.HP.Multiplier.Level ) + ( PlayerData.Stats.Health * PlayerData.HP.Multiplier.Point ) ) )
  411.  
  412.             UpdateInstance(InstanceData,"Stamina", "Max",       PlayerData.Stamina.Base + ((PlayerData.Level.Current * PlayerData.Stamina.Multiplier.Level) + (PlayerData.Stats.Stamina * PlayerData.Stamina.Multiplier.Point)))
  413.             UpdateInstance(InstanceData,"Stats", "Points",          PlayerData.Stats.Points)
  414.             UpdateInstance(InstanceData,nil, "Yen",                 PlayerData.Yen)
  415.  
  416.             UpdateInstance(InstanceData,"Spins", "Common",      PlayerData.Spins.Common)
  417.             UpdateInstance(InstanceData,"Spins", "Uncommon",    PlayerData.Spins.Uncommon)
  418.             UpdateInstance(InstanceData,"Spins", "Rare",            PlayerData.Spins.Rare)
  419.             UpdateInstance(InstanceData,"Spins", "PlusUltra",       PlayerData.Spins.PlusUltra)
  420.  
  421.         end
  422.     end)
  423. end)
  424.  
  425.  
  426. return PlayerDataService
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement