Python Player Logging via HTTP Requests

Hello Guys! I recently made a Python and Lua file in which are used together using the Roblox HTTP service to present to the Python server the current amount of people playing the game. updated in REAL TIME, not waiting 30 seconds for the Roblox one to update, and sometimes it’s off by about 3-20 players, and on big games such as adopt me, it could be off by hundreds.

Python Packages Required:

  • Flask

So here’s what it looks like.

The Lua Script:

local HttpService = game:GetService("HttpService")
local joinUrl = "Hidden/update/join/"
local leaveUrl = "Hidden/update/leave/"

game.Players.PlayerAdded:Connect(function(plr)
	local json = HttpService:GetAsync(joinUrl)
	local data = HttpService:JSONDecode(json)
	local responseTable = data["Return"]
	local responseCode = responseTable[1]
	print(responseCode)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local json = HttpService:GetAsync(leaveUrl)
	local data = HttpService:JSONDecode(json)
	local responseTable = data["Return"]
	local responseCode = responseTable[1]
	print(responseCode)
end)

The Python Script:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    f = open("index.json", "r")
    return f.read()
    f.close()

@app.route('/update/<function>')
def update(function):
    f = open("index.json", "r")
    if function == "join":
      currentplayers = open("player.txt", "r")
      startop = int(currentplayers.read())
      writeplayers = open("player.txt", "w")
      floattowrite = str(startop + 1)
      writeplayers.write(floattowrite)
      print(currentplayers.read())
      currentplayers.close()
      writeplayers.close()
    if function == "leave":
      currentplayers = open("player.txt", "r")
      startop = int(currentplayers.read())
      writeplayers = open("player.txt", "w")
      floattowrite = str(startop - 1)
      writeplayers.write(floattowrite)
      print(currentplayers.read())
      currentplayers.close()
      writeplayers.close()
    playersinserver = open("player.txt", "r")
    print(playersinserver.read())
    playersinserver.close
    return f.read()
    f.close()

if __name__ == "__main__": 
	app.run(
		host='0.0.0.0',
		port=80
  )

The Python output:

Note: The root folder the python file is located in must have a file named “players.txt” which contains the value of 0, nothing more. This is required or the Lua and Python scripts will error with HTTP code 500.

Note: The root folder must also contain a file named “index.json” and it should have the following code:

{
  "Return": [200]
}

I use replit.com for hosting of mine, but due to the fact that replit servers do not have much memory or power, it is recommended to use a private server such as a port forwarded Raspberry Pi to run the Python code for larger games.

Hope you all will find this helpful for your own game!

(@anon61048460 don’t publicly shame my Python code pls lol)

5 Likes

No words (and I’m writting words)
This is amazing!

2 Likes

Thanks! I like seeing that I’m helping people out!

1 Like

Wow nice! this is amazing! thats alot of scripting to

Nah, it wasn’t that bad, the only problem was actually updating a live count of players, as I had to use a file, And with a little more code, you get something like this:

I’m going to put this in community resources with a tutorial. So people can do it themselves.

I wasn’t going to until you pinged me.
you’re lucky im tired today

1 Like

Woah! Cool! I use replit too! My username is “iScriptWithJava”
I use it for basic stuff. Not that complex

1 Like

Wow! That looks really complex. I have learned basic python, but never anything this complex. The powers of python. . . .

2 Likes

Lol. Pings go brrrr.

I’m glad your tied today too.