Easy communication between Roblox server and Python

This tutorial is for advanced users and a minimum knowledge about post/get methods as well as networking is recommended

You will require:

  • Python installed
  • Flask library installed
  • Knowledge about networking, Lua/Python/Html
  • Port forwarding

Today I will show you a simple code that I wrote about how easy you can link a Roblox server to a Python code running on a separate computer.
First things first, lets start with the basic POST/GET principle:
HTTP GET/POST DIAGRAM
The Server will be the host computer which runs the python code, and the Client is our Roblox game.
To make it work, we need to host a website which will receive our requests and pass them to the python code.

For that, Iā€™m gonna be using Flask library, as itā€™s easy to use and provide a lot of support.
To install it, use the following command: pip install flask in the terminal/command prompt.
Next, we need to make the HTML interface for our website the client is gonna connect to.
Hereā€™s a code that I used (it needs to be named ā€œindex.htmlā€ and placed in a folder named ā€˜templatesā€™, next to our python script):

<html>
 <title>
     My http test page
</title>

<body>
    <p>
        Here we have the methods:
        <form method="POST" action="/test"></form>
        <form method="GET" action="/test"></form>
    </p>
</body>

</html>

This code provides a connection between site forms(post/get) and code.
Next, for the back end we have our flask code:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/test', methods=['POST', 'GET'])
def test():
    if request.method == 'GET':
        msg = "this is a response to roblox instance, sent from python"
        print("SENT >>", msg)
        return msg
    print("RECEIVED >>",list(request.form))
    return render_template('index.html')

app.run(host='0.0.0.0',port='1111',debug=False)

Our site is going to be hosted on the local machine(0.0.0.0) on the port 1111.
For testing purposes, you can easily access the website by typing in the browser

http://localhost:1111

Remember! This will only work as long as your script is running!
As you run the script and access your brand new website(which exists only in your LAN for now), you should see something like that:Screenshot_51
Great! Now for the Roblox part.
For the request processing, we will use HttpService, as shown:

http = game:GetService("HttpService")
message = "message sent from roblox"
print("SENT >> "..message)
http:PostAsync("http://localhost:1111/test", message,Enum.HttpContentType.ApplicationUrlEncoded)
print("RECEIVED >> "..http:GetAsync("http://localhost:1111/test"))

If everything is set up correctly, while having the code running on the same machine we have the Roblox Studio open, we will see the following:


Awesome! You made a great job putting them together!

But how do I send data ?
Well, if you need to send some data to your computer, you will need to use the POST method, and this line of code in your roblox script:
http:PostAsync("http://localhost:1111/test", DATA_HERE, Enum.HttpContentType.ApplicationUrlEncoded)

and receive it in the Flask code by doing this
list(request.form)
which will return a list of sent data

If you want to retrieve data from computer, use the GET method, as shown:
http:GetAsync("http://localhost:1111/test")
which will return data you asked for.

Warning! The following part is not recommended to implement due to the fact you are exposing your network to the wide internet! Continue on your own risk!
Now, for the hard part, to make it work in an actual game, we need to change a few things.
To make the Roblox servers be able to access our Flask website, we need to establish a port forwarding feature on you router/modem to be able to access the website from outside your house.
For that, you need to log into your router/modem and set a new Virtual Servers entry with the service port of 1111, IP address to your computer/next router leading to your computer which you need to set up as well. Protocol set to All (UDP and TCP), Status on Enabled aaand Done!
now you should be able to access the website from outside your local network. For the roblox part, will need to change from ā€˜localhostā€™ to your public IP address (which is extremely dangerous), or buy a domain to hide your IP and use it instead of showing your public IP address.
Some routers may encounter issues forwarding your requests. For that you can tweak DMZ(demilitarized zone) settings as well.
Again, exposing your IP is not recommended, so for this tutorial I used it only on localhost.

I hope this tutorial helped you in understanding POST/GET requests and further game creating. :smiley:

30 Likes

This is so helpful, thanks so much! Would it be possible to post data instead of get data?

Thanks again, I am making body tracking now!

I know im a little late, but iā€™m new to python, so how could i go about using a github pages website to host this and request from there?

Sorry Iā€™m replying late, github pages is a static site, which means it can only display a website, and nothing more. I recommend using railway.app for that.

Hope this answers your question.

yeah, i figured it out a while ago and thought i deleted the post, but thanks for the railway.app suggestion!

for some reason flask doesnt want to work, there isnt a version for my python (i tried updating and re-installing, sadly same problem, how can i fix)

The current version of Flask ( 2.1.3) is only available for Python 3.7 or newer, if you do not have python 3.7 or newer installed, you will have to install that in order for Flask to work.

You may also need to specify which version of python you are installing packages too, as some systems will have python 2.X and Python 3.X on them, if this is the case, use the command pip3 install flask

i remember i have 3.10.5, could be wrong so ill reinstall and update you, thanks for your help!

edit : after reinstalling it, didnt work, i even updated pip, idk why i cant do pip install module, its kinda weird or i just dont knowā€¦

If you donā€™t have admin on your PC, you could try installing flask in user mode with the -U flag.

I would also recommend using something like Conda to manage packages, as this should clear up most of the issues you have with installing packages.

idk why but i cant really understand anything from the conda web, i think ill just give up :frowning:

heeeeeeeeeeey dude, I was trying to install pygame today and ran into the same problem, it became so frustrating not being able to install anything, i decided to check everything, i had the correct python setup but the wrong pip, my pip was for 3.8 and i was using 3.10 i installed 3.8 and i can download everything now, thanks for your help tho!

1 Like