Advertisement
Ultimate_69

Untitled

Apr 22nd, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. local ServerScriptService = game:GetService("ServerScriptService")
  2. local ServerStorage = game:GetService("ServerStorage")
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local Players = game:GetService("Players")
  5.  
  6. local ProfileStore = require(ServerScriptService.DataStore.ProfileStore)
  7.  
  8. local PROFILE_TEMPLATE = {
  9. Cash = 0,
  10. Gems = 0,
  11. Fighters = {},
  12. Skins = {},
  13. }
  14.  
  15. local ALLOWED_PLAYER_DATA = {
  16. Cash = 0,
  17. Gems = 0,
  18. Fighters = 0,
  19. Skins = 0,
  20. -- list of data the player is allowed to fetch
  21. }
  22.  
  23. local DataMananger = ServerStorage.ServerBindables.DataManager
  24.  
  25. local PlayerStore = ProfileStore.New("PlayerStore", PROFILE_TEMPLATE)
  26. local Profiles: {[player]: typeof(PlayerStore:StartSessionAsync())} = {}
  27.  
  28. local maxDataMutationTries = 5 -- when changing data, how many tries
  29.  
  30. local dataFetcherRemote = ReplicatedStorage.Remotes.DataFetcher
  31. local moneyChangedRemote = ReplicatedStorage.Remotes.MoneyChanged
  32.  
  33. local function PlayerAdded(player)
  34. -- Start a profile session for this player's data:
  35.  
  36. local profile = PlayerStore:StartSessionAsync(`{player.UserId}`, {
  37. Cancel = function()
  38. return player.Parent ~= Players
  39. end,
  40. })
  41.  
  42. -- Handling new profile session or failure to start it:
  43.  
  44. if profile ~= nil then
  45.  
  46. profile:AddUserId(player.UserId) -- GDPR compliance
  47. profile:Reconcile() -- Fill in missing variables from PROFILE_TEMPLATE (optional)
  48.  
  49. profile.OnSessionEnd:Connect(function()
  50. Profiles[player] = nil
  51. player:Kick(`Profile session end - Please rejoin`)
  52. end)
  53.  
  54. if player.Parent == Players then
  55. Profiles[player] = profile
  56. print(`Profile loaded for {player.DisplayName}!`)
  57. else
  58. -- The player has left before the profile session started
  59. profile:EndSession()
  60. end
  61.  
  62. else
  63. -- This condition should only happen when the Roblox server is shutting down
  64. player:Kick(`Profile load fail - Please rejoin`)
  65. end
  66.  
  67. end
  68.  
  69. -- In case Players have joined the server earlier than this script ran:
  70. for _, player in Players:GetPlayers() do
  71. task.spawn(PlayerAdded, player)
  72. end
  73.  
  74. Players.PlayerAdded:Connect(PlayerAdded)
  75.  
  76. Players.PlayerRemoving:Connect(function(player)
  77. local profile = Profiles[player]
  78. if profile ~= nil then
  79. profile:EndSession()
  80. end
  81. end)
  82.  
  83. DataMananger.Event:Connect(function(player: Player, dataToModify: {any})
  84. print("Updating Data for " .. player.Name .. " to: ")
  85. print(dataToModify)
  86.  
  87. local tries = 0
  88. local success = false
  89.  
  90. while tries < maxDataMutationTries do
  91. if Profiles[player] == nil then
  92. task.wait(2)
  93. tries += 1
  94. else
  95. success = true
  96. break
  97. end
  98. end
  99.  
  100. if not success then
  101. warn("Failed to update data for " .. player.Name)
  102. return
  103. end
  104.  
  105. for i,v in dataToModify do
  106. if not PROFILE_TEMPLATE[i] then continue end -- skip data that isn't in the player's original data
  107.  
  108. if i == "Cash" or i == "Gems" then
  109. moneyChangedRemote:FireClient(player, i, v)
  110. end
  111.  
  112. if Profiles[player].Data[i] then
  113. Profiles[player].Data[i] = v
  114. end
  115. end
  116. end)
  117.  
  118. dataFetcherRemote.OnServerInvoke = function(player, data)
  119. print("Fetching Data for " .. player.Name .. ": " .. data)
  120.  
  121. local tries = 0
  122. local success = false
  123.  
  124. while tries < maxDataMutationTries do
  125. if Profiles[player] == nil then
  126. task.wait(2)
  127. tries += 1
  128. else
  129. success = true
  130. break
  131. end
  132. end
  133.  
  134. if not success then
  135. warn("Failed to fetch data for " .. player.Name)
  136. return nil
  137. end
  138.  
  139. if Profiles[player].Data[data] and ALLOWED_PLAYER_DATA[data] then
  140. return Profiles[player].Data[data]
  141. else
  142. return nil
  143. end
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement