sentencetogame.com

AI Game Maker Multiplayer Leaderboard: The Desync That Only Shows Up With a Second Player

Short answer: a generated game does not become broken when a second player joins. It was already broken, and the second player is the first thing that can see it. Single-player hides every state problem because there is only one copy of the truth, and that copy lives in the tab the player is looking at. Add one more client and you now have two copies that drift apart silently — no error, no crash, no console message, just two people describing different games. There are five places where this shows up, and you can find all five in about fifteen minutes with two browser windows, one stopwatch and no code reading.

The rest of this article is the inspection, not the build. It does not tell you which networking library to pick, because that decision gets made once and this problem happens on every project.


Why single-player passes and two-player fails

Think about what "the game state" means in a generated single-player game. It is a JavaScript object in memory, created once when the page loads, and mutated by input and by a timer. Nothing else ever reads it. If a value goes stale, wrong, or missing, the only observer is the person whose finger is on the button, and they will interpret whatever happens as "the game."

A second player destroys that assumption in one step. Now there are two objects, in two tabs, on two machines. Each one is being mutated by a different set of inputs. For the game to be the same game, every mutation that matters has to travel to the other copy, arrive in the right order, and be applied at a moment when the other copy can accept it.

Generated code almost never does that, and here is the specific reason. A generator writes initialization, not re-entry. It writes the code that sets up a fresh game, because that is the only case it has ever been shown. It does not write the code that puts a second, late, already-existing player into a game that is already in progress, because in the examples the generator learned from, nobody ever arrives late.

That single omission produces all five seams below. They look like five different bugs. They are one bug seen from five angles.

The fifteen-minute two-client inspection

Do this before reading anything else. The setup matters more than the method.

  1. Open your game in two windows on the same machine, side by side. Not two tabs in the same window — two separate windows, both visible at once. You need to see both frames at the same instant. This is the single most important step and the one people skip.
  2. If the game has a "play with a friend" path that produces a room code or a share link, use it properly: one window creates, the other joins. If it does not, skip to the fallback below.
  3. Play for sixty seconds with a stopwatch running. Every time the two windows disagree about anything on screen, write down the second it happened and what differed.
  4. Then have window B close the tab and rejoin while window A keeps playing. Watch what B sees.
  5. Then resize window B so its viewport is roughly half the width of window A. Watch what changes.

You are not looking for a bug. You are looking for the first disagreement and the moment it appeared. The first disagreement tells you which seam is open; the moment tells you which end is authoritative.

Fallback if the game has no join path at all. Then the multiplayer claim is marketing, and the honest reading is that you have a single-player game with a leaderboard. That is a legitimate product — it just is not the thing the page promised. Confirm it in one step: open two windows, and check whether a score submitted in one window appears in the other's board without a refresh. If it does not, there is no shared state at all and seams two through five cannot exist yet.

What you sawWhich seam is openWhat it usually means
Both windows run, both accept input, scores disagreeAuthorityEach client is the boss of its own copy
Window B starts in a different place than A's current positionSpawnStart state was written once, at load
Window B's rejoin resets the whole game for A tooLate joinJoin is implemented as "start over"
Score posts twice, or the board shows a number nobody achievedBoard writeThe write happens on both ends, or on the wrong end
Window B at half width sees a different playfieldViewportCamera and collision are tied to pixel size

Seam 1 — Authority: who is allowed to be right

In a single-player game, input directly mutates state. That is correct and fast. In a two-player game, if both clients keep doing that, you have two independent simulations wearing the same name, and they will diverge the moment anything nondeterministic happens — a random spawn, a timer that fires at a slightly different frame, a floating-point difference across devices.

The tell: both windows respond instantly to their own input, and the two screens disagree within a few seconds of play. There is no lag between press and effect in either window, which is exactly the symptom. Real networked input always has a visible delay on the remote player.

The one-line judgement: if pressing a key in window B changes something in window B before anything changes in window A, B is authoritative for that action. Count how many actions behave that way. If it is more than "my own avatar's cosmetic facing," the seam is open.

You do not need a framework to fix the direction of this. You need one rule written down and enforced: one end decides, the other end displays. Which end that is matters far less than that it is only one. A generated game that lets the host decide is fine. A generated game where each client decides its own score is the reason your leaderboard is meaningless.

The cost of getting this wrong is not visible in the first minute. It shows up as a leaderboard where the top score is a number that no single session could produce — because it was assembled from two clients' local truths.

Seam 2 — Spawn: where the second player starts

Ask a generator for two players and it will usually produce something that looks right: two sprites, two score counters, two sets of controls. What it will not reliably produce is a second starting position that is valid in the world the first player has already changed.

In a fresh game, spawn is a constant. Player one at x=100, player two at x=700. That works at second zero. At second forty, x=700 might be inside a wall the first player built, on top of a hazard that spawned there, or off the edge of a level that has since scrolled.

The test: start a game in window A, move around for thirty seconds, change the world in whatever way the game allows — collect, build, destroy, open — then have window B join. Look at where B appears. Then look at whether B can move.

Three outcomes, three different problems:

B's experienceReading
Appears at a fixed point, can move freelySpawn is a constant. Fine for now, breaks when the world gets dense
Appears at a fixed point, is stuck or dies immediatelySpawn is a constant and the constant is no longer valid
Appears near A, in the state A is inSomeone wrote a real join routine. This is the good case

The fix is not a better constant. It is a validity check at the moment of entry — a short routine that asks "is this position free, and if not, where is the nearest free one," run every time a player enters, not once at load. That is the re-entry pattern the generator skipped.

Seam 3 — Late join: what happens to the game in progress

This is the seam that produces the most spectacular failures, and the easiest one to see.

Have window B join a game that window A has been playing for a minute. Then watch window A. If window A's score resets, its world rebuilds, or its timer jumps back to zero, then "join" was implemented as "start a new game" — and because both clients share the same start routine, starting one restarts both.

The tell is not in window B. It is in window A. That is why this seam survives so long: the person testing is usually looking at the window they just opened.

The underlying problem is a missing distinction between two very different events:

A generated codebase typically has only the first. The second gets faked by calling the first again.

What a correct enter routine has to hand over is not "the starting state" but "the current state, plus enough of the rules that the newcomer can predict what happens next." In practice that means three things travel: the mutable values (positions, scores, timers), the identity of things that were created after load (spawned enemies, placed blocks), and the count of things that were consumed (lives used, items taken). Miss the third and the newcomer sees a world where nothing has been picked up yet.

A one-question test for whether enter exists at all: join a game in progress and check whether the number of remaining lives or resources matches what window A shows. If the newcomer starts with a full set while A is down to one, enter does not exist.

Seam 4 — Board write: the leaderboard records something nobody did

The leaderboard is where the two-client problem becomes permanent, because a leaderboard is the only part of the game that outlives the session. Every mistake in the other four seams gets frozen into a row here.

There are exactly three questions to answer about a generated leaderboard, and the order matters:

  1. Which end writes the row? If both clients write, you get duplicates. If the losing client writes, you get a row for a game that was lost.
  2. What is the row's key? If the key is a timestamp taken on the client, two players can produce the same key. If the key is a score, two equal scores collide and one silently disappears.
  3. What happens when two rows arrive at the same time? If nothing is specified, the answer is "whichever the storage layer saw last," which is not the same as "whichever is higher."

A quick way to see all three at once: have both windows finish a round within a few seconds of each other, then refresh both boards. Count the rows. If the count does not match the number of completed rounds, at least one of the three questions is unanswered.

The judgement that matters here, and the one that separates a working board from a decorative one: a leaderboard that is written by the client is a suggestion, not a record. It can be correct, but its correctness depends on the client behaving, and the client is the one thing you do not control. If the row is written by whichever end has authority, the board inherits the authority decision you already made in seam one — which is the second reason to make that decision explicitly.

One more thing that trips people up: the board and the in-game score are not the same number. The board shows the best of many sessions; the in-game score is one session. If the generated code pushes the in-game score on every frame instead of on the transition out of a round, the board fills with intermediate values and the real best score gets buried. Check by looking for a row that matches a score you saw mid-round.

Seam 5 — Viewport: two screens, two different games

This one is invisible on a desktop test and obvious on a phone, which is why it goes unfixed.

Make window B about half the width of window A. If the playfield in B now shows more or less of the world, or if B's player can reach places A's cannot, then the camera and the collision logic are tied to the pixel size of the viewport rather than to a fixed world size.

In single-player this is harmless — one player, one screen, one consistent set of rules, even if those rules change when the window resizes. In two-player it is a fairness bug: the player with the wider window can see threats earlier and reach further.

The test is one line of observation, not code: at half width, does B see the same amount of world as A, or a different amount? Same amount means the camera is anchored to the world. Different amount means it is anchored to the screen.

The fix has a cost that generated code rarely accounts for: if you anchor the camera to the world, you must decide what happens at the edges of the visible area on the smaller screen. Generated games tend to solve this by cropping, which is fine, or by squashing, which is not.

Why the five seams are one seam

All five come from the same line of reasoning: the generator wrote code for a world with one observer, and every one of the five seams is a place where the number of observers changed.

SeamSingle-observer assumptionWhat breaks when there are two
AuthorityInput mutates state directlyTwo states, two truths
SpawnPosition is a constantConstant is invalid in a changed world
Late joinInitialize == enterEntering restarts the game
Board writeWhoever has the score records itDuplicate or fabricated rows
ViewportScreen size is the worldUnequal rules per player

That is why fixing them one at a time feels like whack-a-mole. The durable fix is to introduce the missing concept — entry as a distinct event from initialization — and then route all five through it. Once "a player entered" is a real event in the code, spawn has a place to check validity, late join has a routine to call, the board has a moment to decide who writes, and the camera has a reason to be re-anchored.

Which seam to fix first

Not the one that looks worst. The one that makes the others testable.

  1. Authority. Until one end is the boss, every other test produces ambiguous results, because you cannot tell whether a difference is a bug or a legitimate local decision.
  2. Late join. This is the re-entry event. Once it exists, spawn and the board have somewhere to live.
  3. Spawn validity. Cheap, and it is the most visible failure to a second player.
  4. Board write. Do this after authority, so the write has a defined owner.
  5. Viewport. Last, because it is the only one that is purely cosmetic-adjacent and the only one where a crop is an acceptable answer.

The order is not about severity. It is about information: each fix should make the next test produce a cleaner reading than the one before it.

What to tell the generator when you ask for it again

If you are going back to the tool rather than the code, ask for the missing concept rather than the symptoms. Symptoms produce patches that fight each other.

None of these are features. They are all descriptions of the event the generator forgot, which is why they tend to be accepted without argument — there is nothing to negotiate about.

Boundaries

This article covers the state divergence that appears the moment a second client exists. It does not cover how to transport the state between clients — that is a transport choice with its own tradeoffs, and it is a separate decision. It does not cover the platform-specific quotas and budgets of any hosted game platform; if you are building inside one, its own documentation is the authority on those limits, not a general article. It does not cover saving a single player's progress between sessions. And it does not cover frame rate: a game can be perfectly synchronized and still run badly, and those are two different investigations.

The one thing worth carrying away: if your generated game has a leaderboard but no join routine, you have a single-player game with a shared scoreboard. That is not a failure. It is just a different product than the one the page claims, and knowing which one you have is the whole point of the two-window test.