[SOLVED] Checking if a game is started Net Code

I’m making an online game and it’s finally starting to come together but I need to make so you can’t join a game in progress. I’ve been trying to figure it out for the last while I have a variable on the server that says weather or not the game has started.

here is the client side code
Redacted(No Longer Relevant)

And here is the server side code
Redacted(No Longer Relevant)

Hi @WilliamBoersma31,

I think you are thinking it correctly, you can have a condition as you say in server that as soon as it turns true it won’t accept/allow any more players to join the server.

For example that can be a max players limit or as soon as the lobby time has elapsed (e.g. 1 minute) and the game starts, you stop accepting more connections to that room/match.

Hey @Leonidas can you give some example code I’ve trying to do that for hours now

Hmm, I am not familiar with your glitch code, but I imagine somewhere in your code you handle the player has joined event right?

You could increment a variable each time a player joins the room, and if it exceeds some value, exit the method and don’t allow him to join (just return I imagine):

// --- example code for player join method
if(playersCount > 10) return;
playersCount ++;

I have a playercount and a function that starts the game if the count is above 2 for 5 secounds (values are extra low for testing)

Where in your script do you start your game? From there you have to communicate to your join function that the game is started. Instead of joining the new player you can show a message that the game is already started.

That’s what I’ve been trying to do but I can’t seem to get it to work

Alright, but where and in which script is your join function and your start function?

The Join is initialize of Network.js and the start is in BR_Start of Network.js

Do you start the game for every player or only when enough players have joined?

after 2 players have joined it waits 5 seconds before starting
(Numbers are adjusted for testing)

I have no multiplayer experience, so forgive me if this is a stupid suggestion, but what about something like below?

socket.on ('playerJoined', function (data) {
    if (!this.Started) {
        self.addPlayer(data);
    }
    else {
        // show 'game is already started' message
    }     
});

I just tried it and it didn’t work.

Probably because you don’t send this.Started to the server?

Actually I do it gets sent along with the movement because I put a lot of player and diagnostic data in there.

So what is the result of console.log(this.Started); at that point?

Oops.

this has to be self.

socket.on ('playerJoined', function (data) {
    if (!self.Started) {
        self.addPlayer(data);
    }
    else {
        // show 'game is already started' message
    }     
});

that just broke the game for everyone else

Even if you don’t do self.loadScene('Error-Full');? Because I don’t see how that part can broke the game for the others.

Anyways, I don’t have enough knowledge about this, so someone else will have to help you.

Ok and yes also I’m fairly certain that on playerJoined Is when another player joins and not when you join though I could be wrong