Hello, this module can help you connect to your MongoDB Datastore from Roblox, with nothing to do from your side, but just initializing the data for the client when they join.
Note: As of September 2021, I have stopped maintaining this module. I won’t be able to assist you with any further issues or code that might break due to future updates.
WHERE TO GET IT
As I told earlier, you will need to get a Roblox Module & The Code for the NodeJS Web server to interact with the MongoDB API. There are others options like MongoDB Realm I believe, but I just like to keep it this way for some reason.
Roblox Module:
NodeJS Server Code:
SETUP PART
I should say this requires some time to setup.
Expand This
Because it is an external database, we will use HttpService internally in the module & the requests will be protected using passwords in base64 encoded form, now people might say this is not secure, if you want you can implement an advance Security system yourself
Step 1: After you download the github source code, you need to make a .env file for the password and usernames for Connecting to Roblox & MongoDB. After that in the .env write down the following:
USERNAME=Testing
PASSWORD=Testing
MONGO_USER=Neil
MONGO_PASS=TestingPass
Basically USERNAME
& PASSWORD
are related to the Roblox Connection Credentials, which you can set yourself and the MONGO_USER
and MONGO_PASS
are related to
MongoDB Connection which you can set too.
Step 2: Go To MongoDB, Create a new Project, and then a new cluster. Then create your Access credentials according to what you had in your .env
file for MongoDB, and then allow access from everywhere in Network Access. Then get your MongoDB Connection string, and paste it down in the mongoose.connect part in server.js and then replace the part where you need to pass the Mongo Username and password, kind of like this example, except the connection string should be yours:
mongoose.connect('mongodb+srv://${mongoUser}:${mongoPass}@cluster0.i3et1.gcp.mongodb.net/testing?retryWrites=true&w=majority'
Step 3: Then first type in npm install
in the root directory, to install all dependencies and Then start your server with npm start
in terminal, in the root directory. I used ngrok tunneling service just for testing, you can host your web server at heroku or something.
Step 4: Once you hosted it, copy the ngrok Url, and then paste it in your Roblox module’s Configuration part’s Server
variable and then also configure the Username and password in there according to the ones you created in your .env
file earlier
HOW TO USE IT
I actually made the Functions quite like Datastore2, but its not a copy of it, I just did because I felt like. So basically, I made only the simple functions for saving / Getting Data right now, so an example usage would be like (Setup using the guide above first.):
local Rmc = require(script.Parent.RobloxMongoConnector)
local players = game.Players
players.PlayerAdded:Connect(function(plr)
local coinStore = Rmc:GetStore("Coins", plr)
print(coinStore:Get(50))
wait(5)
coinStore:Set(100)
print(coinStore:Get(50))
end)
Methods
-
Rmc:GetStore("StoreName", playerInstance)
Basically create or get an existing store, for using the
:Get
and:Set
methods On. -
Store:Get(DefaultValue)
This method accepts a default value to be used if there isn’t data for the player previously.
-
Store:Set(Value)
This method accepts the value to update in the store.
You don’t need to do anything for saving actually, everything is done internally in module
The :Get
method actually is similar to DataStore2’s except its not how it works internally.
:
Why Should You Use MongoDB?
- You can store upto 16 Mb in a Document (Think of it as a key)
- MongoDB is really fast in querying
- Data is stored on the Cloud, and you also get an UI to manage all your data. Which makes it a lot easier.
Downsides
- I think there is only this downside, as for me, MongoDB’s costs increase if your storing alot of data when your game gets larger.
Expand
If you want to know more about what MongoDB is, I actually found a some good articles:
Beginners Guide: MongoDB Basics | MongoDB
Everything About MongoDB — Part 1 | by Sahitya Kumar Suman | Medium