Code is running for everyone instead of one person?

Question: I am making a multiplayer game but I am having an issue with code running when not supposed to. I’m not really sure what’s happening here.

Repl link: https://bulletpartyio.coloredhue.repl.co/


Here’s a video to help explain the problem. When everyone in a lobby is ready it puts them in the game, but then some other code plays when another person tries to lobby.

Looking at your code I saw that the server broadcasts events like lobbyCreated and lobbyFound to all conected socket… and I think this is the cause?

For example, if one client triggers the createLobby event, the server will create a lobby and then emits the event to all clients, so, if a client trigger the findLobby event and finds a lobby, the server will emit the loobyFound event to all clients.

I think that if you want the code to run only for the client that triggered the event, you should use socket.emit() instead of io.sockets.emit().

Idk how to apply that to your code right now (kinda busy) but this should clarify your problem a little

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.