> For the complete documentation index, see [llms.txt](https://praryo.gitbook.io/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://praryo.gitbook.io/documentation/paid-resources/cfx-praryo-subscriptions/usage.md).

# Usage

## Exports

#### Server-Side Exports

**getPlayerSubscriptions**

*This export can be used if you want to retrieve all subscriptions of the target player.*

```lua
// Usage example
RegisterNetEvent("exampleEvent", function()
   local Player = Framework.Functions.GetPlayer(source)
   local currentSubscriptions = exports['cfx-praryo-subscriptions']:getPlayerSubscriptions(Player.PlayerData.source)
	
   print(json.encode(currentSubscriptions, {
     indent = true
   }))
end)

// Output (Array): {"level_1", "level_2"}
// level_1, level_2 comes from the Config.Subscriptions index.
```

**hasPlayerSubscription**

*This export can be used to determine whether a player has a specific subscription.*

```lua
// Usage example
RegisterNetEvent("exampleEvent", function()
   local Player = Framework.Functions.GetPlayer(source)
   local hasSub = exports['cfx-praryo-subscriptions']:hasPlayerSubscription(Player.PlayerData.source, "level_1")
	
   if hasSub then
      print("Subscribed to level_1 subscription.")
   else
      print("Not subscribed to level_1 subscription.")
   end
end)
```

####

**addPlayerSubscription**

*This export can be used to add pending subscriptions using your scripts.*

```lua
// Usage example
RegisterNetEvent("exampleEvent", function()
   local Player = Framework.Functions.GetPlayer(source)
   local success, message, message_type = exports['cfx-praryo-subscriptions']:addPlayerSubscription(Player.PlayerData.source, "level_1")
	
   if success then
      print("Added subscription pending to player")
   else
      print("Failed to add (".. message_type .."): ".. message .."")
   end
end)
```

**removePlayerSubscription**

*This export can be used to remove subscriptions using your scripts.*

```lua
// Usage example
RegisterNetEvent("exampleEvent", function()
   local Player = Framework.Functions.GetPlayer(source)
   local success, message, message_type = exports['cfx-praryo-subscriptions']:removePlayerSubscription(Player.PlayerData.source, "level_1")
	
   if success then
      print("Remove subscription from player")
   else
      print("Failed to remove (".. message_type .."): ".. message .."")
   end
end)
```

#### Client-Side Exports

**getPlayerSubscriptions**

*This export can be used if you want to retrieve all subscriptions of the target player.*

```lua
// Usage example
RegisterNetEvent("exampleEvent", function()
   local currentSubscriptions = exports['cfx-praryo-subscriptions']:getPlayerSubscriptions()
	
   print(json.encode(currentSubscriptions, {
     indent = true
   }))
end)

// Output (Array): {"level_1", "level_2"}
// level_1, level_2 comes from the Config.Subscriptions index.
```

**hasPlayerSubscription**

*This export can be used to determine whether a player has a specific subscription.*

```lua
// Usage example
RegisterNetEvent("exampleEvent", function()
   local hasSub = exports['cfx-praryo-subscriptions']:hasPlayerSubscription("level_1")
	
   if hasSub then
      print("Subscribed to level_1 subscription.")
   else
      print("Not subscribed to level_1 subscription.")
   end
end)
```

## Events

**Server-Side Events**

*Usage:*

<pre class="language-lua"><code class="lang-lua"><strong>-- Event when a player redeem a subscription
</strong><strong>AddEventHandler("cfx-praryo-subscriptions:serverEvents:newSubscription", function(source, subscription)
</strong>   print("[".. source .."] Subscription added: " .. subscription)
end)

-- Event when admin remove a subscription from player
AddEventHandler("cfx-praryo-subscriptions:serverEvents:removeSubscription", function(source, subscription)
   print("[".. source .."] Subscription removed: " .. subscription)
end)
</code></pre>

*Example:*

```lua
AddEventHandler("cfx-praryo-subscriptions:serverEvents:newSubscription", function(source, subscription)
   local Player = Framework.Functions.GetPlayer(source)
	
   if subscription == "level_1" then
      exports.ox_inventory:AddItem(source, 'bread', 4)
   end
end)
```

#### Client-Side Events

*Usage:*

```lua
RegisterNetEvent("cfx-praryo-subscriptions:clientEvents:newSubscription", function(subscription)
   print("Subscription added: " .. subscription)
end)

RegisterNetEvent("cfx-praryo-subscriptions:clientEvents:removeSubscription", function(subscription)
   print("Subscription removed: " .. subscription)
end)
```

*Example:*

```lua
RegisterNetEvent("cfx-praryo-subscriptions:clientEvents:newSubscription", function(subscription)
   if subscription == "level_1" then
      exports.ox_inventory:openInventory('stash', { id = 'level_1_subscription' })
   end
end)
```
