Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --// Services
- -- Create a table to hold all the functions for DataService.
- local DataService = {}
- -- Use game:GetService to access ServerStorage.
- local ServerStorage = game:GetService("ServerStorage")
- -- Require the ProfileService module from ServerStorage.
- local ProfileService = require(ServerStorage.ProfileService)
- --// Globals
- -- Create a table called DefaultData.
- -- DefaultData should include:
- -- - Coins set to 0
- -- - An empty Inventory table
- local DefaultData = {
- Coins = 0;
- Inventory = {
- };
- }
- -- Create a ProfileStore using ProfileService:GetProfileStore.
- -- - Name it "ProfileStore" and pass DefaultData as the default structure.
- local ProfileStore = ProfileService.GetProfileStore("ProfileStore", DefaultData)
- -- Create an empty table called LoadedProfiles to keep track of active player profiles.
- -- - Inside DataService
- DataService.LoadedProfiles = {}
- --// Core
- -- Define a function called Welcome that takes a Player as a parameter.
- -- Inside this function, print "Loaded Player".
- -- We'll call this when someone's loaded in.
- function DataService.welcome(player)
- print("Loaded Player")
- end
- -- Define a function called HandleClient that takes a Player as a parameter.
- -- Inside this function:
- function DataService.HandleClient(player)
- -- Use ProfileStore:LoadProfileAsync to load the player's profile using their UserId (convert it to a string).
- local Profile = ProfileService:LoadProfileAsynce(tostring(player.UserId))
- -- Check if the profile exists (if Profile).
- -- If the profile doesn't exist, kick the player using Player:Kick with a message "Data failed to load!".
- if not Profile then
- player:Kick("Data Failed to load!")
- else
- -- Add the player's UserId to the profile using Profile:AddUserId.
- Profile:AddUserId(player.UserId)
- -- Use Profile:Reconcile to ensure the profile matches DefaultData.
- Profile:Reconcile()
- -- Set up Profile:ListenToRelease to handle profile cleanup when it is released.
- Profile:ListenToRelease(function()
- -- Remove the player's profile from the LoadedProfiles table.
- DataService.LoadedProfiles[player] = nil
- end)
- -- Check if the player is still in the game using Player:IsDescendantOf(game.Players).
- if player:IsDescendantOf(game.Players) then
- -- Store the player's profile in the LoadedProfiles table.
- DataService.LoadedProfiles[player] = Profile
- -- Call the Welcome function for the Player.
- DataService.welcome(player)
- else
- -- If not still in game, release profile.
- Profile:Release()
- end
- end
- end
- -- Define a function called HandleRemovingClient that takes a Player as a parameter.
- -- Inside this function:
- -- - Get the player's profile from the LoadedProfiles table.
- -- - If a profile exists, release it using Profile:Release.
- function DataService.HandleRemovingClient(player)
- local Profile = DataService.LoadedProfiles[player]
- if Profile then
- Profile:Release()
- end
- end
- return DataService
- -- At the end of the script, return the DataService table so other scripts can access it.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement