[Open Sourced] Custom Game Server, Proof of Concept

Overview
This is not a tutorial, it is a quick little concept I whipped up in a few hours that I think a few people might find pretty cool. Before I say anything else, I would like to get my terminology across:

  • game server refers to the external web server being used to host the server code
  • server refers to the 1 player Roblox server, not the player itself
  • player refers to the player playing on the 1 player server

(sorry for the confusing terminology)

Now that I have that over and done with let me explain some of this. You might notice that the game (posted below) has a max player count of one. This is needed for the game to work correctly. The way that the client (Roblox server) works are that every 0.125 seconds, it sends a request to Google Could Platform (you don’t have to use this, it is just what I am using because of how well it auto-scales, more info below). This post request tells the server the players current position and then once the servers updates the player in the table, it returns a table of every player’s position in the game.

I originally sent two HTTP requests every 0.25 seconds, one which told the server the players positon and another which asked for all other player positions, but I then realised that this used double the HTTP requests and I later realised that I could use the method I mentioned about and send requests every 0.125 seconds for a much faster experience, while keeping within the throttle (just). As well as this, I also added tweening so that the player wouldn’t just instantly teleport, which gives the illusion of fast updates when in reality it is relatively slow.

When the server receives the player positions from the server, it simply tweens that players model to the position.

The game server code doesn’t need much explaining, it is incredibly self-explanatory if you have some experience with Node.Js and Express (which you should if you plan on setting this up for yourself)

The Source
The code is hardly optimized and there are a lot of features to add (I haven’t made its that players are removed when they leave yet), but this is something I thought a lot of people’ll find super interesting and cool.

I highly recommend using the Google Cloud app engine to host the game server because of its powerful auto scaling features and ease of use. But you can use whatever cloud provider you prefer.

The game can be found here.

Game server source:

var express = require('express')
var app = express()

var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json())

var players = {}

app.get('/', function(req, res) {
    res.send(JSON.stringify(players))
})

app.post('/updatePlayer', function(req, res) {
    players[req.body.username] = {x: req.body.posX, y: req.body.posY, z: req.body.posZ, username: req.body.username}
    res.send(JSON.stringify(players))
})

app.delete('/clear', function(req, res) {
    players = {}
    res.sendStatus(200)
})

app.set('port', (process.env.PORT || 8080))

app.listen(app.get('port'), () => {
    console.log(`Listening on ${app.get('port')}`)
})

Contributions
Please, by all means, contribute. If you have made a contibution then reply to this thread with the source and I’ll probably add it to the main post.

Conclusion
I hope that some people found this interesting and useful, I had a lot of fun making it. Feel free to give some thoughts and suggestions below!

4 Likes

This topic was automatically closed after 1 minute. New replies are no longer allowed.