Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local FinishedLoading = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"))
- FinishedLoading.Name = "FinishedLoading"
- local FinishedLoadingServer = Instance.new("BindableEvent", game:GetService("ServerScriptService"))
- FinishedLoadingServer.Name = "FinishedLoadingServer"
- local readFirst = {
- ["CustomId"] = {IntType = "StringValue", StartValue = tostring(game:GetService("HttpService"):GenerateGUID(false)), DataFolder = "PlayerData"};
- }
- local DataTable = {
- ["jelly-01"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
- ["jelly-02"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
- ["jelly-03"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
- ["jelly-04"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
- ["jelly-05"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
- ["Items"] = {IntType = "IntValue", StartValue = 0, DataFolder = "PlayerData"};
- ["GD"] = {IntType = "IntValue", StartValue = 10, DataFolder = "PlayerData", ChangeFunction = function(plr, clientValue)
- local clientHeaders = {["X-App-Id"] = "01cu8w", ["X-Authentication"] = tostring(plr:WaitForChild("PlayerData"):WaitForChild("SessionTicket").Value)}
- local postJson = game:GetService("HttpService"):JSONEncode({})
- local endpoint = "https://4b605.playfabapi.com/Client/GetUserInventory"
- --Post method within Roblox
- local encodedResponse = game:GetService("HttpService"):PostAsync(endpoint, postJson, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
- local webServerValue
- --Decodes response
- local response = game:GetService("HttpService"):JSONDecode(encodedResponse)
- if tostring(response.code) == "200" and response.status == "OK" then
- webServerValue = response.data.VirtualCurrency["GD"]
- end
- if webServerValue < clientValue then
- local clientServerDifference = clientValue-webServerValue
- --increment here
- local clientHeaders = {["X-App-Id"] = "01cu8w", ["X-Authentication"] = tostring(plr:WaitForChild("PlayerData"):WaitForChild("SessionTicket").Value)}
- local postJson = game:GetService("HttpService"):JSONEncode({Amount = tostring(clientServerDifference), VirtualCurrency = tostring("GD")})
- local endpoint = "https://4b605.playfabapi.com/Client/AddUserVirtualCurrency"
- --Post method within Roblox
- local encodedResponse = game:GetService("HttpService"):PostAsync(endpoint, postJson, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
- --Decodes response
- local response = game:GetService("HttpService"):JSONDecode(encodedResponse)
- if tostring(response.code) == "200" and response.status == "OK" then
- print("Difference:", clientServerDifference, "NEW VALUE (CLIENT):", clientValue, "OLD VALUE (WEB SERVER):", webServerValue, "NEW VALUE (WEB SERVER):", response.data.Balance)
- end
- end
- end};
- ["PlayFabId"] = {IntType = "StringValue", StartValue = "", DataFolder = "PlayerData"};
- }
- local SessionData = {
- ["SessionTicket"] = {IntType = "StringValue", StartValue = "", DataFolder = "PlayerData", StartFunction = function(Plr)
- --headers, encoded post JSON, and api endpoint
- local headers = {["X-App-Id"] = "01cu8w"}
- local postJson = game:GetService("HttpService"):JSONEncode({CreateAccount = true, CustomId = Plr:WaitForChild("PlayerData"):WaitForChild("CustomId").Value, TitleId = "4B605"}) --This is an account within the system
- local endpoint = "https://4b605.playfabapi.com/Client/LoginWithCustomID"
- --Post method within Roblox
- local encodedResponse = game:GetService("HttpService"):PostAsync(endpoint, postJson, Enum.HttpContentType.ApplicationJson, false, headers)
- --Decodes response
- local response = game:GetService("HttpService"):JSONDecode(encodedResponse)
- if tostring(response.code) == "200" and response.status == "OK" then
- Plr:WaitForChild("PlayerData"):WaitForChild("SessionTicket").Value = tostring(response.data.SessionTicket)
- Plr:WaitForChild("PlayerData"):WaitForChild("PlayFabId").Value = tostring(response.data.PlayFabId)
- if response.data.NewlyCreated then
- --Set Display Name:
- local clientHeaders = {["X-App-Id"] = "01cu8w", ["X-Authentication"] = tostring(response.data.SessionTicket)}
- local postAddDisplayName = game:GetService("HttpService"):JSONEncode({DisplayName = tostring(Plr.Name)}) --This applies a username to playfab acc
- local displayNameEndpoint = "https://4b605.playfabapi.com/Client/UpdateUserTitleDisplayName"
- --Post method within Roblox
- local displayNameEncodedResponse = game:GetService("HttpService"):PostAsync(displayNameEndpoint, postAddDisplayName, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
- --Decodes response
- local displayNameResponse = game:GetService("HttpService"):JSONDecode(displayNameEncodedResponse)
- if tostring(displayNameResponse.code) == "200" and displayNameResponse.status == "OK" then
- print("Successfully set player display name:", displayNameResponse.data.DisplayName)
- end
- --Set Avatar image:
- -- Fetch the thumbnail
- local content = game:GetService("Players"):GetUserThumbnailAsync(Plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
- local postAddAvatarImage = game:GetService("HttpService"):JSONEncode({ImageUrl = tostring(content)}) --This updates image
- local addAvatarImageEndpoint = "https://4b605.playfabapi.com/Client/UpdateAvatarUrl"
- --Post method within Roblox
- local addAvatarImageEncodedResponse = game:GetService("HttpService"):PostAsync(addAvatarImageEndpoint, postAddAvatarImage, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
- --Decodes response
- local addAvatarImageResponse = game:GetService("HttpService"):JSONDecode(addAvatarImageEncodedResponse)
- if tostring(addAvatarImageResponse.code) == "200" and addAvatarImageResponse.status == "OK" then
- print("Successfully set player avatar image", tostring(Plr.Name))
- end
- --Set Player Stats
- local postUpdatePlayerStats = game:GetService("HttpService"):JSONEncode({PlayFabId = tostring(response.data.PlayFabId), Statistics = {{StatisticName = "elite", Value = 0}}}) --This sets initial stats
- local updatePlayerStatsEndpoint = "https://4b605.playfabapi.com/Client/UpdatePlayerStatistics"
- --Post method within Roblox
- local updatePlayerStatsEncodedResponse = game:GetService("HttpService"):PostAsync(updatePlayerStatsEndpoint, postUpdatePlayerStats, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
- --Decodes response
- local updatePlayerStatsResponse = game:GetService("HttpService"):JSONDecode(updatePlayerStatsEncodedResponse)
- if tostring(updatePlayerStatsResponse.code) == "200" and updatePlayerStatsResponse.status == "OK" then
- print("Initiated Player stats", tostring(Plr.Name))
- end
- print("Registered new account.")
- elseif not response.data.NewlyCreated then
- local clientHeaders = {["X-App-Id"] = "01cu8w", ["X-Authentication"] = tostring(response.data.SessionTicket)}
- --Set Avatar image:
- -- Fetch the thumbnail
- local content = game:GetService("Players"):GetUserThumbnailAsync(Plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
- local postAddAvatarImage = game:GetService("HttpService"):JSONEncode({ImageUrl = tostring(content)}) --This applies a username to playfab acc
- local addAvatarImageEndpoint = "https://4b605.playfabapi.com/Client/UpdateAvatarUrl"
- --Post method within Roblox
- local addAvatarImageEncodedResponse = game:GetService("HttpService"):PostAsync(addAvatarImageEndpoint, postAddAvatarImage, Enum.HttpContentType.ApplicationJson, false, clientHeaders)
- --Decodes response
- local addAvatarImageResponse = game:GetService("HttpService"):JSONDecode(addAvatarImageEncodedResponse)
- if tostring(addAvatarImageResponse.code) == "200" and addAvatarImageResponse.status == "OK" then
- print("Successfully set player avatar image", tostring(Plr.Name))
- end
- print("Logged into an account")
- end
- end
- end};
- }
- local function ReadData(Plr)
- --print("SessionData Loaded")
- for Key, Data in pairs(DataTable) do
- local DataVal = Instance.new(Data.IntType, Plr:WaitForChild(tostring(Data.DataFolder)))
- DataVal.Name = tostring(Key)
- local MainDataStore = game:GetService("DataStoreService"):GetDataStore(tostring(Key))
- local PlayerSpecificData = MainDataStore:GetAsync(Plr.UserId)
- if PlayerSpecificData == nil then
- local Success, Err = pcall(function()
- MainDataStore:SetAsync(Plr.UserId, Data.StartValue)
- end)
- if Success then
- DataVal.Value = Data.StartValue
- --print("Data Initiated: " .. tostring(Key), Data.StartValue)
- if Data.StartFunction ~= nil then
- Data.StartFunction(Plr, DataVal.Value)
- end
- end
- elseif PlayerSpecificData ~= nil then
- local Success, Err = pcall(function()
- DataVal.Value = MainDataStore:GetAsync(Plr.UserId)
- end)
- if Success then
- if Data.StartFunction ~= nil then
- Data.StartFunction(Plr, MainDataStore:GetAsync(Plr.UserId))
- end
- end
- end
- DataVal:GetPropertyChangedSignal("Value"):Connect(function()
- local Success, Err = pcall(function()
- MainDataStore:SetAsync(Plr.UserId, DataVal.Value)
- end)
- if Success then
- --print("Data Changed: " .. tostring(Key), DataVal.Value)
- if Data.ChangeFunction ~= nil then
- Data.ChangeFunction(Plr, DataVal.Value)
- end
- end
- end)
- --print(tostring(Key) .. " [DataRead]")
- end
- for Key, Data in pairs(SessionData) do
- local DataVal = Instance.new(Data.IntType, Plr:WaitForChild(tostring(Data.DataFolder)))
- DataVal.Name = tostring(Key)
- if Data.StartFunction then
- Data.StartFunction(Plr)
- end
- end
- --print("Data Initiation Completed.")
- FinishedLoading:FireAllClients()
- FinishedLoadingServer:Fire(Plr)
- wait()
- end
- local function ReadDataFirst(Plr)
- --print("SessionData Loaded")
- for Key, Data in pairs(readFirst) do
- local DataVal = Instance.new(Data.IntType, Plr:WaitForChild(tostring(Data.DataFolder)))
- DataVal.Name = tostring(Key)
- local MainDataStore = game:GetService("DataStoreService"):GetDataStore(tostring(Key))
- local PlayerSpecificData = MainDataStore:GetAsync(Plr.UserId)
- if PlayerSpecificData == nil then
- local Success, Err = pcall(function()
- MainDataStore:SetAsync(Plr.UserId, Data.StartValue)
- end)
- if Success then
- DataVal.Value = Data.StartValue
- --print("Data Initiated: " .. tostring(Key), Data.StartValue)
- if Data.StartFunction ~= nil then
- Data.StartFunction(Plr, DataVal.Value)
- end
- end
- elseif PlayerSpecificData ~= nil then
- local Success, Err = pcall(function()
- DataVal.Value = MainDataStore:GetAsync(Plr.UserId)
- end)
- if Success then
- if Data.StartFunction ~= nil then
- Data.StartFunction(Plr, MainDataStore:GetAsync(Plr.UserId))
- end
- end
- end
- DataVal:GetPropertyChangedSignal("Value"):Connect(function()
- local Success, Err = pcall(function()
- MainDataStore:SetAsync(Plr.UserId, DataVal.Value)
- end)
- if Success then
- --print("Data Changed: " .. tostring(Key), DataVal.Value)
- if Data.ChangeFunction ~= nil then
- Data.ChangeFunction(Plr, DataVal.Value)
- end
- end
- end)
- --print(tostring(Key) .. " [DataRead]")
- end
- ReadData(Plr)
- end
- game:GetService("Players").PlayerAdded:Connect(function(Plr)
- Instance.new('IntValue', Plr).Name = "PlayerData"
- local character = Plr.CharacterAdded:Wait()
- ReadDataFirst(Plr)
- --first read data then
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement