# Storing Roblox Game Data in MongoDB Atlas Using Rongo

## Introduction 

This article will walk you through setting up and using Rongo in your Roblox games, and storing your data in MongoDB Atlas. Rongo is a custom SDK that uses the MongoDB Atlas Data API.

We’ll walk through the process of inserting Rongo into your game, setting Rongo up with MongoDB, and finally, using Rongo to store a player's data in MongoDB Atlas as an alternative to Roblox’s DataStores. Note that this library is open source and not supported by MongoDB.

## Prerequisites

Before you can start using Rongo, there are a few things you’ll need to do.

If you have not already, you’ll need to install Roblox Studio and create a new experience.

Next, you’ll need to set up a MongoDB Atlas Account, which you can learn how to do using MongoDB’s Getting Started with Atlas article.

Once you have set up your MongoDB Atlas cluster, follow the article on getting started with the MongoDB Data API.

After you’ve done all of the above steps, you can get started with installing Rongo into your game!

## Installing Rongo into your game

The installation of Rongo is fairly simple. However, you have a couple of ways to do it!

### Using Roblox’s Toolbox (recommended)

First, head to the Rongo library page and press the **Get** button. After that, you can press the “Try in studio” button, which will open Roblox Studio and insert the module into Studio.

If you wish to insert the module into a specific experience, then open Roblox Studio, load your experience, and navigate to the **View** tab. Click on **Toolbox**, navigate to the **Inventory** tab of the Toolbox, and locate Rongo. Or, search for it in the toolbox and then drag it into the viewport.

### Downloading the Rongo model

You can download Rongo from our Github page by visiting our releases page and downloading the **Rongo.rbxm** file or the **Rongo.lua** file. After you have downloaded either of the files, open  Roblox studio and load your experience. Next, navigate to the **View** tab and open the **Explorer** window. You can then right click on **ServerScriptService** and press the **Insert from file** button. Once you’ve pressed the **Insert from file** button, locate the Rongo file and insert it into Roblox Studio.

## Setting up Rongo in your game

First of all, you’ll need to ensure that the Rongo module is placed in **ServerScriptService**.

Next, you must enable the **Allow HTTP Requests **setting in your games settings (security tab).

After you have done the above two steps, create a script in **ServerScriptService** and paste in the example code below.

```lua
local Rongo = require(game:GetService("ServerScriptService"):WaitForChild("Rongo"))

local Client = Rongo.new(YOUR_API_ID, YOUR_API_KEY)
local Cluster = Client:GetCluster("ExampleCluster")
local Database = Cluster:GetDatabase("ExampleDatabase")
local Collection = Database:GetCollection("ExampleCollection")
```

The above code will allow you to modify your collection by adding data, removing data, and fetching data.

You’ll need to replace the arguments with the correct data to ensure it works correctly!

Refer to our documentation for more information on the functions.

To fetch data, you can use this example code:

```lua
local Result = Collection:FindOne({"Name"] = "Value"})
```

You can then print the result to the console using print(Result).

Once you’ve gotten the script setup, you’re all good to go and you can move onto the next section of this article!

## Using Rongo to save player data

This section will teach you how to save a player's data when they join and leave your game!

We’ll be using the script we created in the previous section as a base for the new script.

First of all, we’re going to create a function which will be fired whenever the player joins the game. This function will load the players data if they’ve had their data saved before!

```lua
Players.PlayerAdded:Connect(function(Player: Player)
--// Setup player leaderstats
local Leaderstats = Instance.new("Folder")
Leaderstats.Parent = Player
Leaderstats.Name = "leaderstats"

local Gold = Instance.new("IntValue")
Gold.Parent = Leaderstats
Gold.Name = "Gold"
Gold.Value = 0

--// Fetch data from MongoDB
local success, data = pcall(function()
return Collection:FindOne({["userId"] = Player.UserId})
end)

--// If an error occurs, warn in console
if not success then
warn("Failed to fetch player data from MongoDB")
return
end

--// Check if data is valid
if data and data["playerGold"] then
--// Set player gold leaderstat value
Gold.Value = data["playerGold"]
end

--// Give player +5 gold each time they join
Gold.Value += 5
end)
```

In the script above, it will first create a leaderstats folder and gold value when the player joins, which will appear in the player list. Next, it will fetch the player data and set the value of the player’s gold to the saved value in the collection. And finally, it will give the player an additional five golds each time they join.

Next, we’ll make a function to save the player’s data whenever they leave the game.

```lua
Players.PlayerRemoving:Connect(function(Player: Player)
--// Get player gold
local Leaderstats = Player:WaitForChild("leaderstats")
local Gold = Leaderstats:WaitForChild("Gold")

--// Update player gold in database
local success, data = pcall(function()
return Collection:UpdateOne({["userId"] = Player.UserId},
{
["userId"] = Player.UserId,
["playerGold"] = Gold.Value
}
, true)
end)
end)
```

This function will first fetch the player's gold in game and then update it in MongoDB with the upsert value set to true, so it will insert a new document in case the player has not had their data saved before.

You can now test it in game and see the data updated in MongoDB once you leave!

If you’d like a more vanilla Roblox DataStore experience, you can also use [MongoStore, which is built on top of Rongo and has identical functions to Roblox’s DataStoreService.

Here is the full script used for this article:

```lua
local Rongo = require(game:GetService("ServerScriptService"):WaitForChild("Rongo"))
local Client = Rongo.new("MY_API_ID", "MY_API_KEY")
local Cluster = Client:GetCluster("Cluster0")
local Database = Cluster:GetDatabase("ExampleDatabase")
local Collection = Database:GetCollection("ExampleCollection")

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player: Player)
--// Setup player leaderstats
local Leaderstats = Instance.new("Folder")
Leaderstats.Parent = Player
Leaderstats.Name = "leaderstats"

local Gold = Instance.new("IntValue")
Gold.Parent = Leaderstats
Gold.Name = "Gold"
Gold.Value = 0

--// Fetch data from MongoDB
local success, data = pcall(function()
return Collection:FindOne({"userId"] = Player.UserId})
end)

--// If an error occurs, warn in console
if not success then
warn("Failed to fetch player data from MongoDB")
return
end

--// Check if data is valid
if data and data["playerGold"] then
--// Set player gold leaderstat value
Gold.Value = data["playerGold"]
end

--// Give player +5 gold each time they join
Gold.Value += 5
end)

Players.PlayerRemoving:Connect(function(Player: Player)
--// Get player gold
local Leaderstats = Player:WaitForChild("leaderstats")
local Gold = Leaderstats:WaitForChild("Gold")

--// Update player gold in database
local success, data = pcall(function()
return Collection:UpdateOne({["userId"] = Player.UserId},
{
["userId"] = Player.UserId,
["playerGold"] = Gold.Value
}
, true)
end)
end)
```

## Conclusion

In summary, Rongo makes it seamless to store your Roblox game data in MongoDB Atlas. You can use it for whatever need be, from player data stores to fetching data from your website's database.

Learn more about Rongo on the [Roblox Developer Forum thread. View Rongo’s source code on our Github page.