Skip to content

playtime-tracker never starts sessions: scalar_async misuse, client-only event #31

Closed

Description

@TMHSDigital

Summary

The playtime-tracker example never records any playtime. startSession passes a Lua callback as the third argument of exports.oxmysql:scalar_async, which is not a callback parameter, so sessions[src] is never set. In standalone mode, sessions are also started from playerSpawned, an event that only fires on the client.

Where

  • examples/playtime-tracker/server/main.lua:88-98 (startSession, scalar_async call)
  • examples/playtime-tracker/server/main.lua:110-114 (standalone AddEventHandler('playerSpawned', ...))
  • examples/playtime-tracker/server/main.lua:56-58 (savePlaytime returns early without a session)
  • examples/playtime-tracker/server/main.lua:138-146 (/playtime "still loading" branch)

Details

exports.oxmysql:scalar_async(
    'SELECT seconds FROM player_playtime WHERE identifier = ?',
    { identifier },
    function(existing)
        sessions[src] = { ... }
    end
)

In oxmysql the _async exports have the signature (query, parameters, invokingResource) and return a promise. The Lua function is treated as the invoking resource name and is never called. Because sessions[src] is never assigned, savePlaytime always returns early, the periodic flush iterates an empty table, nothing is ever written to player_playtime, and /playtime always answers "Playtime is still loading".

Separately, the standalone branch registers AddEventHandler('playerSpawned', ...) in a server script. playerSpawned is raised locally on the client by spawnmanager and is never delivered to the server, so in standalone mode no session would start even after the export call is fixed. Under every framework, players already online when the resource (re)starts are also never tracked until they reconnect.

Impact

The example's single feature does nothing under ESX, QBCore or standalone, and users copying the pattern get silent no-ops. The README says standalone mode is supported.

Steps to reproduce

  1. Start the resource with oxmysql and the SQL from examples/playtime-tracker/sql/ applied.
  2. Join and wait past Config.SaveIntervalMinutes.
  3. Run /playtime: it always shows "Playtime is still loading". The player_playtime table stays empty.
  4. With Config.Framework = 'standalone', no session is created on join either.

Suggested fix

  • Use the callback export, exports.oxmysql:scalar(query, params, function(existing) ... end), or load @oxmysql/lib/MySQL.lua and use local existing = MySQL.scalar.await(query, params).
  • Guard the async result so a callback that resolves after the player has dropped does not recreate sessions[src].
  • For standalone, start sessions from the server-side playerJoining event instead of playerSpawned.
  • In an onResourceStart handler for this resource, loop over GetPlayers() and start sessions for players already online.

Acceptance criteria

  • After joining and waiting one save interval, player_playtime has a row for the player and /playtime reports a total.
  • Standalone mode starts a session on join.
  • Restarting the resource resumes tracking for players already online.
  • A player who drops before the DB read completes does not leave a stale session.

Getting started

  • Relevant files: examples/playtime-tracker/server/main.lua, examples/playtime-tracker/README.md
  • How to run checks: there is no test suite yet; run the MCP server with cd mcp-server && pip install -r requirements.txt && python server.py, and CI checks live in .github/workflows/validate.yml
  • Related docs: skills/database-integration/SKILL.md, oxmysql documentation

Filed by repo-audit/full-audit at 701824d. Severity: high. Effort: S. audit-key: bug|examples/playtime-tracker/server/main.lua|scalar_async

Activity

  1. added
    bug
    good first issue
    on Nov 21, 2026
  2. TMHSDigital commented on Nov 21, 2026

    @TMHSDigital
    OwnerAuthor

    Fixed in 3f2dbb0.

    startSession now uses the callback-style exports.oxmysql:scalar and ignores results for players who dropped or were already loaded; standalone mode starts sessions on the server-side playerJoining event; players already online are picked up on resource start; README example corrected.

    Verified: Reviewed against the oxmysql export signatures. Not run on a live FXServer (no server available in this environment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggood first issueseverity: high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions