Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ServerScriptService = game:GetService("ServerScriptService")
- local ServerStorage = game:GetService("ServerStorage")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Players = game:GetService("Players")
- local ProfileStore = require(ServerScriptService.DataStore.ProfileStore)
- local PROFILE_TEMPLATE = {
- Cash = 0,
- Gems = 0,
- Fighters = {},
- Skins = {},
- }
- local ALLOWED_PLAYER_DATA = {
- Cash = 0,
- Gems = 0,
- Fighters = 0,
- Skins = 0,
- -- list of data the player is allowed to fetch
- }
- local DataMananger = ServerStorage.ServerBindables.DataManager
- local PlayerStore = ProfileStore.New("PlayerStore", PROFILE_TEMPLATE)
- local Profiles: {[player]: typeof(PlayerStore:StartSessionAsync())} = {}
- local maxDataMutationTries = 5 -- when changing data, how many tries
- local dataFetcherRemote = ReplicatedStorage.Remotes.DataFetcher
- local moneyChangedRemote = ReplicatedStorage.Remotes.MoneyChanged
- local function PlayerAdded(player)
- -- Start a profile session for this player's data:
- local profile = PlayerStore:StartSessionAsync(`{player.UserId}`, {
- Cancel = function()
- return player.Parent ~= Players
- end,
- })
- -- Handling new profile session or failure to start it:
- if profile ~= nil then
- profile:AddUserId(player.UserId) -- GDPR compliance
- profile:Reconcile() -- Fill in missing variables from PROFILE_TEMPLATE (optional)
- profile.OnSessionEnd:Connect(function()
- Profiles[player] = nil
- player:Kick(`Profile session end - Please rejoin`)
- end)
- if player.Parent == Players then
- Profiles[player] = profile
- print(`Profile loaded for {player.DisplayName}!`)
- else
- -- The player has left before the profile session started
- profile:EndSession()
- end
- else
- -- This condition should only happen when the Roblox server is shutting down
- player:Kick(`Profile load fail - Please rejoin`)
- end
- end
- -- In case Players have joined the server earlier than this script ran:
- for _, player in Players:GetPlayers() do
- task.spawn(PlayerAdded, player)
- end
- Players.PlayerAdded:Connect(PlayerAdded)
- Players.PlayerRemoving:Connect(function(player)
- local profile = Profiles[player]
- if profile ~= nil then
- profile:EndSession()
- end
- end)
- DataMananger.Event:Connect(function(player: Player, dataToModify: {any})
- print("Updating Data for " .. player.Name .. " to: ")
- print(dataToModify)
- local tries = 0
- local success = false
- while tries < maxDataMutationTries do
- if Profiles[player] == nil then
- task.wait(2)
- tries += 1
- else
- success = true
- break
- end
- end
- if not success then
- warn("Failed to update data for " .. player.Name)
- return
- end
- for i,v in dataToModify do
- if not PROFILE_TEMPLATE[i] then continue end -- skip data that isn't in the player's original data
- if i == "Cash" or i == "Gems" then
- moneyChangedRemote:FireClient(player, i, v)
- end
- if Profiles[player].Data[i] then
- Profiles[player].Data[i] = v
- end
- end
- end)
- dataFetcherRemote.OnServerInvoke = function(player, data)
- print("Fetching Data for " .. player.Name .. ": " .. data)
- local tries = 0
- local success = false
- while tries < maxDataMutationTries do
- if Profiles[player] == nil then
- task.wait(2)
- tries += 1
- else
- success = true
- break
- end
- end
- if not success then
- warn("Failed to fetch data for " .. player.Name)
- return nil
- end
- if Profiles[player].Data[data] and ALLOWED_PLAYER_DATA[data] then
- return Profiles[player].Data[data]
- else
- return nil
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement