xbread / SantosRP

MIT License
7 stars 5 forks source link

mysql not saving #15

Open michaelsv2 opened 2 weeks ago

michaelsv2 commented 2 weeks ago

Hey i got some issue with MYSQL saving data for ex: it saving skills, lost and found, but not cars and player inventory. getting this error: [ERROR] gamemodes/santosrp/gamemode/sv_mysql_player.lua:386: bad argument #1 to 'TableToJSON' (table expected, got string)

  1. TableToJSON - [C]:-1
    1. InsdupCharacterDataStoreVar - gamemodes/santosrp/gamemode/sv_mysql_player.lua:386
    2. unknown - gamemodes/santosrp/gamemode/sv_mysql_player.lua:540
      1. CommitPlayerDiffs - gamemodes/santosrp/gamemode/sv_mysql_player.lua:434
      2. PlayerDisconnected - gamemodes/santosrp/gamemode/sv_mysql.lua:46
      3. unknown - gamemodes/santosrp/gamemode/init.lua:149

also all the cars are like 10mph max

chadreed93 commented 2 weeks ago

The error you're encountering, bad argument #1 to 'TableToJSON' (table expected, got string), suggests that the TableToJSON function is being passed a string instead of a table, which is what it expects.

Problem Area:

The error occurs in the InsdupCharacterDataStoreVar function, which is defined as:

function GM.SQL:InsdupCharacterDataStoreVar( intPoolID, intCharID, strVar, vaValue )
    local strValue = util.TableToJSON( vaValue )
    self:LogPlayerQuery( intCharID, ([[INSERT INTO character_data_store (character_id, `key`, value) VALUES (%d, '%s', '%s') ON DUPLICATE KEY UPDATE value='%s']]):format(intCharID, SQLStr(strVar, true), SQLStr(strValue, true), SQLStr(strValue, true)) )
    self:PooledQueryWrite( intPoolID, ([[INSERT INTO character_data_store (character_id, `key`, value) VALUES (%d, '%s', '%s') ON DUPLICATE KEY UPDATE value='%s']]):format(intCharID, SQLStr(strVar, true), SQLStr(strValue, true), SQLStr(strValue, true)) )
end

The issue arises when vaValue is passed as a string instead of a table.

Possible Fix:

Before converting vaValue to JSON, ensure it is a table. If it is not, you should handle it appropriately—either by converting the string to a table or by adjusting the input data. Here's a modified version of the function with added checks:

function GM.SQL:InsdupCharacterDataStoreVar(intPoolID, intCharID, strVar, vaValue)
    -- Check if vaValue is a table; if not, convert it or handle the error
    if type(vaValue) ~= "table" then
        print("Warning: vaValue is not a table. Converting to table or handling error.")
        -- Attempt to convert to a table (if vaValue is a valid JSON string)
        local parsedValue = util.JSONToTable(vaValue)
        if parsedValue then
            vaValue = parsedValue
        else
            -- Handle the case where vaValue is neither a table nor a valid JSON string
            vaValue = { ["data"] = vaValue }
        end
    end

    local strValue = util.TableToJSON(vaValue)
    self:LogPlayerQuery(intCharID, ([[INSERT INTO character_data_store (character_id, `key`, value) VALUES (%d, '%s', '%s') ON DUPLICATE KEY UPDATE value='%s']]):format(intCharID, SQLStr(strVar, true), SQLStr(strValue, true), SQLStr(strValue, true)))
    self:PooledQueryWrite(intPoolID, ([[INSERT INTO character_data_store (character_id, `key`, value) VALUES (%d, '%s', '%s') ON DUPLICATE KEY UPDATE value='%s']]):format(intCharID, SQLStr(strVar, true), SQLStr(strValue, true), SQLStr(strValue, true)))
end

Explanation:

This should prevent the error and allow the function to handle various input types more gracefully. If you have specific cases where vaValue is intended to be a string, you might need additional logic to handle those separately.

michaelsv2 commented 2 weeks ago

Thank you! ,it worked, the database started to save everything. getting this: Warning: vaValue is not a table. Converting to table or handling error.

also after i restarted my server its stuck on waiting for game data.

[ERROR] gamemodes/santosrp/gamemode/sv_networking.lua:230: bad argument #1 to 'func' (string expected, got table)

  1. func - [C]:-1
    1. SendFullGameVarUpdate - gamemodes/santosrp/gamemode/sv_networking.lua:230
    2. unknown - gamemodes/santosrp/gamemode/sv_characters.lua:199
xbread commented 1 week ago

The error you're encountering, bad argument #1 to 'TableToJSON' (table expected, got string), suggests that the TableToJSON function is being passed a string instead of a table, which is what it expects.

Problem Area:

The error occurs in the InsdupCharacterDataStoreVar function, which is defined as:

function GM.SQL:InsdupCharacterDataStoreVar( intPoolID, intCharID, strVar, vaValue )
    local strValue = util.TableToJSON( vaValue )
    self:LogPlayerQuery( intCharID, ([[INSERT INTO character_data_store (character_id, `key`, value) VALUES (%d, '%s', '%s') ON DUPLICATE KEY UPDATE value='%s']]):format(intCharID, SQLStr(strVar, true), SQLStr(strValue, true), SQLStr(strValue, true)) )
    self:PooledQueryWrite( intPoolID, ([[INSERT INTO character_data_store (character_id, `key`, value) VALUES (%d, '%s', '%s') ON DUPLICATE KEY UPDATE value='%s']]):format(intCharID, SQLStr(strVar, true), SQLStr(strValue, true), SQLStr(strValue, true)) )
end

The issue arises when vaValue is passed as a string instead of a table.

Possible Fix:

Before converting vaValue to JSON, ensure it is a table. If it is not, you should handle it appropriately—either by converting the string to a table or by adjusting the input data. Here's a modified version of the function with added checks:

function GM.SQL:InsdupCharacterDataStoreVar(intPoolID, intCharID, strVar, vaValue)
    -- Check if vaValue is a table; if not, convert it or handle the error
    if type(vaValue) ~= "table" then
        print("Warning: vaValue is not a table. Converting to table or handling error.")
        -- Attempt to convert to a table (if vaValue is a valid JSON string)
        local parsedValue = util.JSONToTable(vaValue)
        if parsedValue then
            vaValue = parsedValue
        else
            -- Handle the case where vaValue is neither a table nor a valid JSON string
            vaValue = { ["data"] = vaValue }
        end
    end

    local strValue = util.TableToJSON(vaValue)
    self:LogPlayerQuery(intCharID, ([[INSERT INTO character_data_store (character_id, `key`, value) VALUES (%d, '%s', '%s') ON DUPLICATE KEY UPDATE value='%s']]):format(intCharID, SQLStr(strVar, true), SQLStr(strValue, true), SQLStr(strValue, true)))
    self:PooledQueryWrite(intPoolID, ([[INSERT INTO character_data_store (character_id, `key`, value) VALUES (%d, '%s', '%s') ON DUPLICATE KEY UPDATE value='%s']]):format(intCharID, SQLStr(strVar, true), SQLStr(strValue, true), SQLStr(strValue, true)))
end

Explanation:

  • Type Checking: The function now checks if vaValue is a table before passing it to TableToJSON.
  • Conversion Attempt: If vaValue is not a table, the function attempts to parse it as JSON, assuming it might be a JSON string.
  • Fallback Handling: If the conversion fails, it wraps the value in a table to ensure the TableToJSON function has the correct input.

This should prevent the error and allow the function to handle various input types more gracefully. If you have specific cases where vaValue is intended to be a string, you might need additional logic to handle those separately.

is this chatgpt? lol

xbread commented 1 week ago

Thank you! ,it worked, the database started to save everything. getting this: Warning: vaValue is not a table. Converting to table or handling error.

also after i restarted my server its stuck on waiting for game data.

[ERROR] gamemodes/santosrp/gamemode/sv_networking.lua:230: bad argument #1 to 'func' (string expected, got table)

  1. func - [C]:-1
  2. SendFullGameVarUpdate - gamemodes/santosrp/gamemode/sv_networking.lua:230
    1. unknown - gamemodes/santosrp/gamemode/sv_characters.lua:199

As for your error I can try and provide as much support as possible unfortunately, I no longer have garry's mod installed nor do I feel like starting up a dev server but, the last time I tested this gamemode was more recently and I had no issues but, just add me on discord breadxiv

Also: the cars being slow/spinning is something that I want to fix with your help if possible the issue has been going on for a while and I had it fixed previously but never pushed anything to the repo.