Ask coding questions

← Back to all posts
Help! I need help!
MatthewX (153)

Hi! I need help with saving numbers and data since for some reason doesn't let me use local storage :(. Also, I have been thinking about how multiplayer games are made. How would I start to make a multiplayer game? I am planning on using javascript and Html I need help with setting up servers and idk how to set up servers. Please help.

Answered by Codeverse (159) [earned 5 cycles]
View Answer
Codeverse (159)

Hello @MatthewX, here is how you can make a multiplayer game. Servers can be made in Python, Java, JavaScript, etc, but for this occasion, I would prefer to use Node.js. Here is how to do Node.js: Node.js tutorial. Next, you have to connect Node.js with and Express or other packages. Your server show like something like this.

const express = require('express');
const socket = require('');

//Uses the express function derived from your express variable.
const app = express();
//3000 is the server name
const server = app.listen(3000);
const io = socket(server);


io.on('connection', socket => {console.log('New connection!');});

Now that you have set up your server, you can work on the gameplay for the game you are making. After that, you want to connect the server and the client together, and here is an example of it: Shared Drawing Canvas. Now you if you want to save to a database, and you don't want to use replit.db, then you can learn NeDB. Here is a tutorial: NeDB Tutorial | Saving to a Database and NeDB Tutorial | Querying the Database. Hope you didn't get bored and learned something from this!

MatthewX (153)

@Codeverse Hi! Thanks for the great advice! I have a question as to how I connect node.js with other packages. Do I download it or do I make a file and import it? If I have to import it how would I do that? Thanks.

tussiez (1666)

@MatthewX handles package installation.
No need to download/make a file - just add require('module') to your code.

If running your own server, run npm i moduleName to install the module

Codeverse (159)

@tussiez But remember, you have to do Npm Init first.