Advertisement
ChickenApples

Ugh

Feb 27th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. --//Modules
  2. local DataService = require(game.ServerScriptService.Services.DataService);
  3. local Schema = require(game.ServerScriptService.Services.Schema);
  4. local QuestModule = require(script.Parent.QuestController.Quest);
  5.  
  6. --//Public Vars
  7. local DsName = "MainDs_v10";
  8. local PlayersAllowedToSave = {};
  9. local TypeConversions = {
  10. ["string"] = "StringValue",
  11. ["boolean"] = "BoolValue",
  12. ["number"] = "NumberValue"
  13. }
  14.  
  15. --//Functions
  16.  
  17. function formatJSON(json)
  18. local depth = 0
  19. local str = ""
  20.  
  21. for i in json:gmatch(".") do
  22. if i == "{" then
  23. depth = depth + 1
  24. str = str..(" "):rep(depth - 1)..i.."\n"..(" "):rep(depth)
  25. elseif i == "}" then
  26. depth = depth - 1
  27. str = str.."\n"..(" "):rep(depth)..i
  28. elseif i == "," then
  29. str = str..i.."\n"..(" "):rep(depth)
  30. else
  31. str = str..i
  32. end
  33. end
  34.  
  35. return str:sub(2)
  36. end
  37.  
  38. local function GetQuestByName(QuestName)
  39. for i, v in pairs(QuestModule) do
  40. if v[QuestName] then
  41. return v[QuestName]
  42. end
  43. end
  44. end
  45.  
  46. local function ConvertSchemaToObj(Schema)
  47. local PlayerStats = Instance.new("Folder");
  48. PlayerStats.Name = "PlayerStats";
  49.  
  50. for Key, Value in pairs(Schema) do
  51. if (typeof(Value) ~= "table") then
  52. local ValueObj = Instance.new(TypeConversions[typeof(Value)]);
  53. ValueObj.Value = Value;
  54. ValueObj.Name = Key;
  55. ValueObj.Parent = PlayerStats;
  56. else
  57. local ParentObj = Instance.new("Folder");
  58. ParentObj.Name = Key;
  59.  
  60. for Key2, Value2 in pairs(Value) do
  61. if ParentObj.Name == 'QuestFolder' then
  62. local CurrectQuest = Instance.new("Folder")
  63. CurrectQuest.Name = Key2
  64. CurrectQuest.Parent = ParentObj
  65.  
  66. for Key3, Value3 in pairs(Value2) do
  67. if typeof(Value3) == "number" then
  68. local ValueObj = Instance.new("DoubleConstrainedValue");
  69. ValueObj.Value = Value3
  70. ValueObj.Name = Key3;
  71. ValueObj.Parent = CurrectQuest;
  72. ValueObj.MaxValue = GetQuestByName(Key2).QuestRequirements[Key3]
  73. else
  74. local ValueObj = Instance.new(TypeConversions[typeof(Value3)]);
  75. ValueObj.Value = Value3
  76. ValueObj.Name = Key3;
  77. ValueObj.Parent = CurrectQuest;
  78. end
  79. end
  80. else
  81. local ValueObj = Instance.new(TypeConversions[typeof(Value2)]);
  82. ValueObj.Value = Value2
  83. ValueObj.Name = Key2;
  84. ValueObj.Parent = ParentObj;
  85. end
  86. end
  87. ParentObj.Parent = PlayerStats;
  88. end
  89. end
  90. --print("Loaded")
  91. return PlayerStats;
  92. end
  93.  
  94. local function ConvertObjToSchema(PlayerStats)
  95. local Schema = {};
  96.  
  97. for i,v in pairs(PlayerStats:GetChildren()) do
  98. if (v:IsA("Folder")) then
  99. local DataToAdd = {};
  100.  
  101. for _,obj in pairs(v:GetChildren()) do
  102. if (table.getn(obj:GetChildren()) > 0) then
  103. DataToAdd[obj.Name] = {};
  104.  
  105. if obj:IsA("Folder") then
  106. for _, Value in pairs(obj:GetChildren()) do
  107. DataToAdd[obj.Name][Value.Name] = Value.Value;
  108. end
  109. else
  110. DataToAdd[obj.Name] = obj.Value
  111. end
  112. else
  113. DataToAdd[obj.Name] = obj.Value
  114. end
  115. end
  116.  
  117. Schema[v.Name] = DataToAdd;
  118. else
  119. Schema[v.Name] = v.Value;
  120. end
  121. end
  122. print(formatJSON(game:GetService("HttpService"):JSONEncode(Schema)))
  123. return Schema;
  124. end
  125.  
  126.  
  127. --//Events
  128. game.Players.PlayerAdded:Connect(function(plr)
  129. local Data = DataService:GetAsync(DsName, plr.UserId) or Schema;
  130. local PlayerStats = ConvertSchemaToObj(Data);
  131.  
  132. PlayerStats.Parent = plr;
  133. PlayersAllowedToSave[plr.Name] = true;
  134.  
  135. repeat
  136. local PlayerData = ConvertObjToSchema(PlayerStats);
  137. DataService:SetAsync(DsName, plr.UserId, PlayerData);
  138. wait(50)
  139. until not plr
  140. end)
  141.  
  142. game.Players.PlayerRemoving:Connect(function(plr)
  143. if (PlayersAllowedToSave[plr.Name]) then
  144. PlayersAllowedToSave[plr.Name] = nil;
  145.  
  146. local PlayerStats = plr.PlayerStats;
  147. local Schema = ConvertObjToSchema(PlayerStats);
  148. DataService:SetAsync(DsName, plr.UserId, Schema);
  149. end
  150. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement