Advertisement
Alexplazz

Untitled

Nov 28th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.03 KB | None | 0 0
  1. --// Services
  2. -- Create a table to hold all the functions for DataService.
  3. local DataService = {}
  4. -- Use game:GetService to access ServerStorage.
  5. local ServerStorage = game:GetService("ServerStorage")
  6. -- Require the ProfileService module from ServerStorage.
  7. local ProfileService = require(ServerStorage.ProfileService)
  8. --// Globals
  9. -- Create a table called DefaultData.
  10. -- DefaultData should include:
  11. --    - Coins set to 0
  12. --    - An empty Inventory table
  13. local DefaultData = {
  14.     Coins = 0;
  15.     Inventory = {
  16.    
  17.     };
  18. }
  19.  
  20. -- Create a ProfileStore using ProfileService:GetProfileStore.
  21. --    - Name it "ProfileStore" and pass DefaultData as the default structure.
  22. local ProfileStore = ProfileService.GetProfileStore("ProfileStore", DefaultData)
  23. -- Create an empty table called LoadedProfiles to keep track of active player profiles.
  24. --    - Inside DataService
  25. DataService.LoadedProfiles = {}
  26. --// Core
  27. -- Define a function called Welcome that takes a Player as a parameter.
  28. -- Inside this function, print "Loaded Player".
  29. -- We'll call this when someone's loaded in.
  30. function DataService.welcome(player)
  31.     print("Loaded Player")
  32. end
  33. -- Define a function called HandleClient that takes a Player as a parameter.
  34. -- Inside this function:
  35.  
  36. function DataService.HandleClient(player)
  37.     -- Use ProfileStore:LoadProfileAsync to load the player's profile using their UserId (convert it to a string).
  38.     local Profile = ProfileService:LoadProfileAsynce(tostring(player.UserId))
  39.     -- Check if the profile exists (if Profile).
  40.     -- If the profile doesn't exist, kick the player using Player:Kick with a message "Data failed to load!".
  41.     if not Profile then
  42.         player:Kick("Data Failed to load!")
  43.     else
  44.         -- Add the player's UserId to the profile using Profile:AddUserId.
  45.         Profile:AddUserId(player.UserId)
  46.         -- Use Profile:Reconcile to ensure the profile matches DefaultData.
  47.         Profile:Reconcile()
  48.         -- Set up Profile:ListenToRelease to handle profile cleanup when it is released.
  49.         Profile:ListenToRelease(function()
  50.             -- Remove the player's profile from the LoadedProfiles table.
  51.             DataService.LoadedProfiles[player] = nil
  52.         end)
  53.        
  54.         -- Check if the player is still in the game using Player:IsDescendantOf(game.Players).
  55.         if player:IsDescendantOf(game.Players) then
  56.             -- Store the player's profile in the LoadedProfiles table.
  57.             DataService.LoadedProfiles[player] = Profile
  58.            
  59.             -- Call the Welcome function for the Player.
  60.             DataService.welcome(player)
  61.         else
  62.             -- If not still in game, release profile.
  63.             Profile:Release()
  64.         end
  65.  
  66.     end
  67.  
  68. end
  69. -- Define a function called HandleRemovingClient that takes a Player as a parameter.
  70. -- Inside this function:
  71. --    - Get the player's profile from the LoadedProfiles table.
  72. --    - If a profile exists, release it using Profile:Release.
  73. function DataService.HandleRemovingClient(player)
  74.     local Profile = DataService.LoadedProfiles[player]
  75.     if Profile then
  76.         Profile:Release()
  77.     end
  78. end
  79.  
  80. return DataService
  81. -- At the end of the script, return the DataService table so other scripts can access it.
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement