Intuitive Player Logging Tool [Python Programming]

Python Player Logging Tool

By Crcoli737

Introduction

This player logging tool logs game visits and current player count in a Python console, it requires a little bit of work to get set up, but I think it’s worth it. Some uses of this tool are:

  • Easy Player and Troll Logging
  • Current Player Count (Updates Instantly)
  • Easy-to-use!

This resource thread includes a tutorial in which uses third-party tools and websites as an easy route, I recommend you use a cloud provider or personal server if you have more than 5k-10k active users as some of the tools and websites shown in the tutorial below do not have absurd lot of CPU power or System Memory.

Credits and Third-Party Sites Used

  • Crcoli737 (Creator)
  • REPL.IT (Cloud Hosting)
  • Roblox HTTP Service (Roblox Server Communication)
  • Lua 5.1 Roblox Variant (Roblox Server Programming)
  • Python 3.8 (REPL.IT Programming)
  • Python Flask (Web Server Hosting on REPL.IT)

Tutorial

Step 1 - Server Script

Firstly we will create a Script in game.ServerScriptService, you may name it whatever you like.
image
Inside of the script you should put the following Lua 5.1 code:

local HttpService = game:GetService("HttpService")
local joinUrl = ""
local leaveUrl = ""

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

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

I know the joinUrl and leaveUrl are put blank, we will come back to that later.

Step 2 - REPL.IT Account

For this step, you will need a free REPL.IT account, it is free and they do not sell or trade your personal information to anyone. You can find the REPL.IT website here.

In order to sign up, locate the “Sign Up” button in the upper right corner of the screen and click it.
image
There, you will create a username, enter your E-Mail, and create a password, or for a quicker sign-up, use one of the quick sign-up buttons located below the text fields, they look like this.
image
If you are signing up, you will have to answer a few questions, such as “What are your favorite programming languages” just answer the questions accordingly, they shouldn’t affect anything for what we are doing.

Once you are finished signing up, you should see a page that looks similar to this (my details scribbled out for privacy).

Step 3 - Creating a REPL

Once you see this page, click the blue “New repl” icon, you should then see a page that looks like this.
image
You should find the Python button and select it, to help you find it, you can just type Python into the search bar.

It will automatically generate a weird, unique name of which you can either keep or name it something else.

Once you have created the REPL by clicking the “Create repl” button, you should see a screen that looks like the one that I took a screenshot of below. Note: I have dark mode on.

I know it looks overwhelming, but I will help you through what to do.

First, we will need the main Python code, which is listed below…

from flask import Flask, render_template
import psutil

app = Flask(__name__)

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

@app.route('/update/<function>/<plr>/<display>')
def update(function, plr, display):
    global status
    playertarget = plr
    global playerdisplayname
    playerdisplayname = display
    f = open("index.json", "r")
    if function == "join":
      status = "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":
      status = "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(), "Players | Username:", playertarget, "| Display Name:", playerdisplayname, "| Action:", status, "| Server RAM Usage:", str(psutil.virtual_memory().percent)+"%")
    playersinserver.close
    return f.read()
    f.close()

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

Put this code in the file named “main.py” then we will test the code and make sure that everything is set-up by clicking the run button. It look something like this.

image

On the black console output on the right you should see some works and text flicker, thats simply the REPL downloading and adding the required packages to the REPL poetry.

Eventually you should see a white screen with large black text and you will most likely think that you’ve done something wrong. YOU HAVEN’T! It should look something like this.

It works! The reason it shows an error is that we told the server what to do, but what we told it to do doesn’t exist… yet.

Step 4 - Writing JSON and TXT files

So now we are going to add the required files to the REPL. This will also get rid of the ISE error that we are receiving.

The first file that we are going to add is a JSON file which is what is returned to the Roblox Server when it talks to our REPL. Fun fact: JSON stands for JavaScript Object Notation and will be turned into a Lua table by the Roblox Server.

To do this, click the icon shown below on the bar on the far left, of the screen. It should be to the right of the word “Files.” Do not click the folder icon. It won’t work!
image

In the rectangular box, type “index.json” followed by the enter key. Hooray! You just created a file.

Now in the text editor box where our Python code previously was is now empty and looks like this.


In this file, you will want to put the following code.

{
  "Return": [200]
}

This code is what is returned to the server once the transaction is complete.

Now we want to create another file, this time it should titled “player.txt”, this is what will store the current number of players. It uses a file as variables will be wiped once the Python code is stopped, where the text file won’t.

We use the same process that we did for creating the index.json file, just naming “player.txt” instead of “index.json” we will see the same empty box like we saw above. This time we just want to type “0” in the text editor, as that is the current number of players. Your screen should look like this.

Now we are pretty much done! Just a few more things.

Step 5 - REPL code testing

Now click Stop, then run Run again! You shouldn’t see an Internal Server Error now and instead it should look something like this.

Step 6 - Roblox Server configuration

Now we should return back to studio, where at the top of our code we see two string variables, they look like this.

local joinUrl = ""
local leaveUrl = ""

We need to fill these strings in with the URL of our REPL. Luckily, this is very easy to do. Now go back to the repl, at the top of that white box, there should be a URL, mine and your’s will be different, but it should look something like this.
image
Copy that URL and paste it in the Join URL, here is on of the most important parts, make sure to append “/update/join/” to the end of your URL, so your URL should look something like this.

local joinUrl = "https://ShadyCompleteOpenlook.crcoli7307.repl.co/update/join/"

Now do the same for the leaveUrl variable, except for putting “/update/join/” put “/update/leave/” on the end. They should look something like this together.

local joinUrl = "https://ShadyCompleteOpenlook.crcoli7307.repl.co/update/join/"
local leaveUrl = "https://ShadyCompleteOpenlook.crcoli7307.repl.co/update/leave/"

And you’re done!

Step 7 - Test Running it all

Now test play your roblox game. While in the game, go back to your repl. In the output you should see something like this!

Once you leave the game, you should see another log below it, it should look like this.

And this will count every single player that joins your game! It’ll also show a live count of players that are in your game.

Conclusion

Overall, this is a great solution to Medium-Large size games that have many trollers. Instead of having a Moderator in each of your servers which is basically impossible for larger games, you can have one or two moderators looking through these console logs handling exploit or troll reports. You can see the current time in which the event occurred as well in the Red text below the log, which will help get an estimated time (if not exact time) of which the attack occurred.

Happy Moderating!

By Crcoli737

4 Likes

Update Log

This post contains a log of all updates to this Resource.

Newest Updates

  • 06/27/2021 - Original Post Released

Oldest Updates

In addition, if you would prefer to not open the repl just to see output, you can open up the /__logs path to display only output.

For example:

https://ShadyCompleteOpenlook.crcoli7307.repl.co/__logs
3 Likes

Yeah. I’ll have to look into that. Thanks for your feedback.

Why would you want to log players? Seems very unnecessary to me.

1 Like

Well the roblox one is only updated very 30 seconds, so if someone plays for 20 seconds it may not count it. You also can use the player logging feature to help cut down on trollers as you can have one or two moderators watching the log to see if a unknown troller or supervised player joins the game. Heck, you could just use it for seeing when a friend joins your game.

1 Like