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
Server
local ServerStorage = game:GetService("ServerStorage")
local ProfileManager = require(ServerStorage.ProfileManager)

ProfileManager.ProfileLoaded:Connect(function(profile)
    task.delay(15, function()
        profile.Data.Value.cash += 50
		
        -- Add a new plane
        local planesLength = #profile.Data.Value.Inventory.Planes
        profile.Data.Value.inventory.planes[planesLength + 1] = "Boeing 777X"
    end)
end)
Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Wizgoose = require(ReplicatedStorage.Wizgoose.Client)

local playerData = Wizgoose.get("player-data.".. Players.LocalPlayer.UserId)

playerData:Changed("cash"):Connect(function(cash)
    print("$".. cash)
end)

playerData:ItemAddedIn("inventory.planes"):Connect(function(planeName)
    print(planeName.. " has been added")
end)

Last updated