# ProfileService

### Libraries

* [ProfileService](https://www.roblox.com/library/5331689994/ProfileService)
* [FastSignal](https://gist.github.com/stravant/7a11ea1147d742463b116a3793e112cb)
* [Wizgoose](https://create.roblox.com/marketplace/asset/10207465581/)

### Source Code

{% code title="Server Module" %}

```lua
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
```

{% endcode %}

{% code title="Server" %}

```lua
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)
```

{% endcode %}

{% code title="Client" %}

```lua
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)
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://moonward.gitbook.io/wizgoose/examples/profileservice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
