ProfileService

Libraries

Source Code

Server Module
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ProfileService = require(ReplicatedStorage.ProfileService)
local Wizgoose = require(ReplicatedStorage.Wizgoose.Server)
local FastSignal = require(ReplicatedStorage.FastSignal)

local profileStore = ProfileService.GetProfileStore("player-data", {
    cash = 100,
    inventory = {
        planes = {"Boeing 747", "Airbus A380"}
    }
})

local ProfileManager = {}
ProfileManager.Profiles = {}
ProfileManager.ProfileLoaded = FastSignal.new()

local function playerJoined(player)
    local key = tostring(player.UserId)
	
    local profile = profileStore:LoadProfileAsync(key, "ForceLoad")
    if not profile then
        player:Kick("Failed to load your data, try joining again.")
        return
    end

    if not player:IsDescendantOf(Players) then
        profile:Release()
        return
    end

    profile:ListenToRelease(function()
        ProfileManager.Profiles[player] = nil
        player:Kick()
    end)

    ProfileManager.Profiles[player] = {
        raw = profile,
        data = Wizgoose.new(
            "player-data.".. player.UserId,
            profile.Data,
            {player}
        ),
        player = player
    }

    ProfileManager.ProfileLoaded:Fire(ProfileManager.Profiles[player])
end

local function playerLeft(player)
    local profile = ProfileManager.Profiles[player]
    if not profile then
        return
    end

    print(profile.Data)
	
    profile.raw:Release()
    profile.data:Destroy() -- IMPORTANT: Destroy the Wizgoose object
    ProfileManager.Profiles[player] = nil -- Clean up the table since we are no longer using it
end

for _, player in Players:GetPlayers() do
    playerJoined(player)
end

Players.PlayerAdded:Connect(playerJoined)
Players.PlayerRemoving:Connect(playerLeft)

return ProfileManager

Last updated