Advertisement
DeveloperCodes

ServerScriptService.DataSystem

Jan 22nd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.54 KB | None | 0 0
  1.  
  2. local FinishedLoading = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"))
  3. FinishedLoading.Name = "FinishedLoading"
  4.  
  5. local FinishedLoadingServer = Instance.new("BindableEvent", game:GetService("ServerScriptService"))
  6. FinishedLoadingServer.Name = "FinishedLoadingServer"
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. local readFirst = {
  17.     ["CustomId"] = {IntType = "StringValue", StartValue = tostring(game:GetService("HttpService"):GenerateGUID(false)), DataFolder = "PlayerData"};
  18. }
  19.  
  20. local DataTable = {
  21.     ["jelly-01"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
  22.     ["jelly-02"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
  23.     ["jelly-03"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
  24.     ["jelly-04"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
  25.     ["jelly-05"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
  26.     ["Items"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
  27.     ["GD"] = {IntType = "IntValue", StartValue = 10, DataFolder = "PlayerData", ChangeFunction = function(plr, clientValue)
  28.         local clientHeaders = {["X-App-Id"] = "01cu8w", ["X-Authentication"] = tostring(plr:WaitForChild("PlayerData"):WaitForChild("SessionTicket").Value)}
  29.         local postJson = game:GetService("HttpService"):JSONEncode({})
  30.         local endpoint = "https://4b605.playfabapi.com/Client/GetUserInventory"
  31.        
  32.         --Post method within Roblox
  33.         local encodedResponse = game:GetService("HttpService"):PostAsync(endpoint, postJson, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
  34.        
  35.        
  36.        
  37.         local webServerValue
  38.         --Decodes response
  39.         local response = game:GetService("HttpService"):JSONDecode(encodedResponse)
  40.         if tostring(response.code) == "200" and response.status == "OK" then
  41.             webServerValue = response.data.VirtualCurrency["GD"]
  42.         end
  43.        
  44.         if webServerValue < clientValue then
  45.             local clientServerDifference = clientValue-webServerValue
  46.            
  47.             --increment here
  48.            
  49.             local clientHeaders = {["X-App-Id"] = "01cu8w", ["X-Authentication"] = tostring(plr:WaitForChild("PlayerData"):WaitForChild("SessionTicket").Value)}
  50.             local postJson = game:GetService("HttpService"):JSONEncode({Amount = tostring(clientServerDifference), VirtualCurrency = tostring("GD")})
  51.             local endpoint = "https://4b605.playfabapi.com/Client/AddUserVirtualCurrency"
  52.            
  53.             --Post method within Roblox
  54.             local encodedResponse = game:GetService("HttpService"):PostAsync(endpoint, postJson, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
  55.            
  56.             --Decodes response
  57.             local response = game:GetService("HttpService"):JSONDecode(encodedResponse)
  58.             if tostring(response.code) == "200" and response.status == "OK" then
  59.                 print("Difference:", clientServerDifference, "NEW VALUE (CLIENT):", clientValue, "OLD VALUE (WEB SERVER):", webServerValue, "NEW VALUE (WEB SERVER):", response.data.Balance)
  60.             end
  61.         end
  62.     end};
  63.     ["PlayFabId"] = {IntType = "StringValue", StartValue = "", DataFolder = "PlayerData"};
  64. }
  65. local SessionData = {
  66.     ["SessionTicket"] = {IntType = "StringValue", StartValue = "", DataFolder = "PlayerData", StartFunction = function(Plr)
  67.         --headers, encoded post JSON, and api endpoint
  68.         local headers = {["X-App-Id"] = "01cu8w"}
  69.         local postJson = game:GetService("HttpService"):JSONEncode({CreateAccount = true, CustomId = Plr:WaitForChild("PlayerData"):WaitForChild("CustomId").Value, TitleId = "4B605"}) --This is an account within the system
  70.         local endpoint = "https://4b605.playfabapi.com/Client/LoginWithCustomID"
  71.        
  72.         --Post method within Roblox
  73.         local encodedResponse = game:GetService("HttpService"):PostAsync(endpoint, postJson, Enum.HttpContentType.ApplicationJson, false, headers)
  74.        
  75.         --Decodes response
  76.         local response = game:GetService("HttpService"):JSONDecode(encodedResponse)
  77.         if tostring(response.code) == "200" and response.status == "OK" then
  78.             Plr:WaitForChild("PlayerData"):WaitForChild("SessionTicket").Value = tostring(response.data.SessionTicket)
  79.             Plr:WaitForChild("PlayerData"):WaitForChild("PlayFabId").Value = tostring(response.data.PlayFabId)
  80.             if response.data.NewlyCreated then
  81.                
  82.                 --Set Display Name:
  83.                
  84.                 local clientHeaders = {["X-App-Id"] = "01cu8w", ["X-Authentication"] = tostring(response.data.SessionTicket)}
  85.                 local postAddDisplayName = game:GetService("HttpService"):JSONEncode({DisplayName = tostring(Plr.Name)}) --This applies a username to playfab acc
  86.                 local displayNameEndpoint = "https://4b605.playfabapi.com/Client/UpdateUserTitleDisplayName"
  87.                 --Post method within Roblox
  88.                 local displayNameEncodedResponse = game:GetService("HttpService"):PostAsync(displayNameEndpoint, postAddDisplayName, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
  89.                
  90.                 --Decodes response
  91.                 local displayNameResponse = game:GetService("HttpService"):JSONDecode(displayNameEncodedResponse)
  92.                 if tostring(displayNameResponse.code) == "200" and displayNameResponse.status == "OK" then
  93.                     print("Successfully set player display name:", displayNameResponse.data.DisplayName)
  94.                 end
  95.                
  96.                 --Set Avatar image:
  97.                
  98.                
  99.                 -- Fetch the thumbnail
  100.                 local content = game:GetService("Players"):GetUserThumbnailAsync(Plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
  101.                
  102.                 local postAddAvatarImage = game:GetService("HttpService"):JSONEncode({ImageUrl = tostring(content)}) --This updates image
  103.                 local addAvatarImageEndpoint = "https://4b605.playfabapi.com/Client/UpdateAvatarUrl"
  104.                 --Post method within Roblox
  105.                 local addAvatarImageEncodedResponse = game:GetService("HttpService"):PostAsync(addAvatarImageEndpoint, postAddAvatarImage, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
  106.                
  107.                 --Decodes response
  108.                 local addAvatarImageResponse = game:GetService("HttpService"):JSONDecode(addAvatarImageEncodedResponse)
  109.                 if tostring(addAvatarImageResponse.code) == "200" and addAvatarImageResponse.status == "OK" then
  110.                     print("Successfully set player avatar image", tostring(Plr.Name))
  111.                 end
  112.                
  113.                
  114.                 --Set Player Stats
  115.                
  116.                 local postUpdatePlayerStats = game:GetService("HttpService"):JSONEncode({PlayFabId = tostring(response.data.PlayFabId), Statistics = {{StatisticName = "elite", Value = 0}}}) --This sets initial stats
  117.                 local updatePlayerStatsEndpoint = "https://4b605.playfabapi.com/Client/UpdatePlayerStatistics"
  118.                 --Post method within Roblox
  119.                 local updatePlayerStatsEncodedResponse = game:GetService("HttpService"):PostAsync(updatePlayerStatsEndpoint, postUpdatePlayerStats, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
  120.                
  121.                 --Decodes response
  122.                 local updatePlayerStatsResponse = game:GetService("HttpService"):JSONDecode(updatePlayerStatsEncodedResponse)
  123.                 if tostring(updatePlayerStatsResponse.code) == "200" and updatePlayerStatsResponse.status == "OK" then
  124.                     print("Initiated Player stats", tostring(Plr.Name))
  125.                 end
  126.                
  127.                 print("Registered new account.")
  128.             elseif not response.data.NewlyCreated then
  129.                 local clientHeaders = {["X-App-Id"] = "01cu8w", ["X-Authentication"] = tostring(response.data.SessionTicket)}
  130.                 --Set Avatar image:
  131.                
  132.                
  133.                 -- Fetch the thumbnail
  134.                 local content = game:GetService("Players"):GetUserThumbnailAsync(Plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
  135.                
  136.                 local postAddAvatarImage = game:GetService("HttpService"):JSONEncode({ImageUrl = tostring(content)}) --This applies a username to playfab acc
  137.                 local addAvatarImageEndpoint = "https://4b605.playfabapi.com/Client/UpdateAvatarUrl"
  138.                 --Post method within Roblox
  139.                 local addAvatarImageEncodedResponse = game:GetService("HttpService"):PostAsync(addAvatarImageEndpoint, postAddAvatarImage, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
  140.                
  141.                 --Decodes response
  142.                 local addAvatarImageResponse = game:GetService("HttpService"):JSONDecode(addAvatarImageEncodedResponse)
  143.                 if tostring(addAvatarImageResponse.code) == "200" and addAvatarImageResponse.status == "OK" then
  144.                     print("Successfully set player avatar image", tostring(Plr.Name))
  145.                 end
  146.                
  147.                 print("Logged into an account")
  148.             end
  149.         end
  150.     end};
  151. }
  152.  
  153.  
  154. local function ReadData(Plr)
  155.     --print("SessionData Loaded")
  156.     for Key, Data in pairs(DataTable) do
  157.         local DataVal = Instance.new(Data.IntType, Plr:WaitForChild(tostring(Data.DataFolder)))
  158.         DataVal.Name = tostring(Key)
  159.         local MainDataStore = game:GetService("DataStoreService"):GetDataStore(tostring(Key))
  160.         local PlayerSpecificData = MainDataStore:GetAsync(Plr.UserId)
  161.         if PlayerSpecificData == nil then
  162.             local Success, Err = pcall(function()
  163.                 MainDataStore:SetAsync(Plr.UserId, Data.StartValue)
  164.             end)
  165.             if Success then
  166.                 DataVal.Value = Data.StartValue
  167.                 --print("Data Initiated: " .. tostring(Key), Data.StartValue)
  168.                 if Data.StartFunction ~= nil then
  169.                     Data.StartFunction(Plr, DataVal.Value)
  170.                 end
  171.             end
  172.         elseif PlayerSpecificData ~= nil then
  173.             local Success, Err = pcall(function()
  174.                 DataVal.Value = MainDataStore:GetAsync(Plr.UserId)
  175.             end)
  176.            
  177.             if Success then
  178.                 if Data.StartFunction ~= nil then
  179.                     Data.StartFunction(Plr, MainDataStore:GetAsync(Plr.UserId))
  180.                 end
  181.             end
  182.         end
  183.         DataVal:GetPropertyChangedSignal("Value"):Connect(function()
  184.             local Success, Err = pcall(function()
  185.                 MainDataStore:SetAsync(Plr.UserId, DataVal.Value)
  186.             end)
  187.             if Success then
  188.                 --print("Data Changed: " .. tostring(Key), DataVal.Value)
  189.                 if Data.ChangeFunction ~= nil then
  190.                     Data.ChangeFunction(Plr, DataVal.Value)
  191.                 end
  192.             end
  193.         end)
  194.         --print(tostring(Key) .. " [DataRead]")
  195.     end
  196.     for Key, Data in pairs(SessionData) do
  197.         local DataVal = Instance.new(Data.IntType, Plr:WaitForChild(tostring(Data.DataFolder)))
  198.         DataVal.Name = tostring(Key)
  199.         if Data.StartFunction then
  200.             Data.StartFunction(Plr)
  201.         end
  202.     end
  203.     --print("Data Initiation Completed.")
  204.     FinishedLoading:FireAllClients()
  205.     FinishedLoadingServer:Fire(Plr)
  206.     wait()
  207. end
  208.  
  209. local function ReadDataFirst(Plr)
  210.     --print("SessionData Loaded")
  211.     for Key, Data in pairs(readFirst) do
  212.         local DataVal = Instance.new(Data.IntType, Plr:WaitForChild(tostring(Data.DataFolder)))
  213.         DataVal.Name = tostring(Key)
  214.         local MainDataStore = game:GetService("DataStoreService"):GetDataStore(tostring(Key))
  215.         local PlayerSpecificData = MainDataStore:GetAsync(Plr.UserId)
  216.         if PlayerSpecificData == nil then
  217.             local Success, Err = pcall(function()
  218.                 MainDataStore:SetAsync(Plr.UserId, Data.StartValue)
  219.             end)
  220.             if Success then
  221.                 DataVal.Value = Data.StartValue
  222.                 --print("Data Initiated: " .. tostring(Key), Data.StartValue)
  223.                 if Data.StartFunction ~= nil then
  224.                     Data.StartFunction(Plr, DataVal.Value)
  225.                 end
  226.             end
  227.         elseif PlayerSpecificData ~= nil then
  228.             local Success, Err = pcall(function()
  229.                 DataVal.Value = MainDataStore:GetAsync(Plr.UserId)
  230.             end)
  231.            
  232.             if Success then
  233.                 if Data.StartFunction ~= nil then
  234.                     Data.StartFunction(Plr, MainDataStore:GetAsync(Plr.UserId))
  235.                 end
  236.             end
  237.         end
  238.         DataVal:GetPropertyChangedSignal("Value"):Connect(function()
  239.             local Success, Err = pcall(function()
  240.                 MainDataStore:SetAsync(Plr.UserId, DataVal.Value)
  241.             end)
  242.             if Success then
  243.                 --print("Data Changed: " .. tostring(Key), DataVal.Value)
  244.                 if Data.ChangeFunction ~= nil then
  245.                     Data.ChangeFunction(Plr, DataVal.Value)
  246.                 end
  247.             end
  248.         end)
  249.         --print(tostring(Key) .. " [DataRead]")
  250.     end
  251.     ReadData(Plr)
  252. end
  253.  
  254.  
  255. game:GetService("Players").PlayerAdded:Connect(function(Plr)
  256.     Instance.new('IntValue', Plr).Name = "PlayerData"
  257.     local character = Plr.CharacterAdded:Wait()
  258.     ReadDataFirst(Plr)
  259.     --first read data then
  260. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement