Sending data to database generating error attempt to index a nil value (global MySQL)

Hello all,
I have been lurking here for a while and setting up quietly a private server.
I have tried to make the timetrials script (release here) send the data it gathered (winner race, vehicle, race) to a local SQL server. I have had alot of issues already but have finally reached a blocker.

Error message
error attempt to index a nil value (global MySQL)
Error occurs when finished a race.

The code generating it

                    -- Save time and play sound for finish line
                    local finishTime = (GetGameTimer() - raceState.startTime)
                    PlaySoundFrontend(-1, "ScreenFlash", "WastedSounds")
                    
                    -- Get vehicle name and create score
                    local aheadVehHash = GetEntityModel(GetVehiclePedIsUsing(GetPlayerPed(-1)))
                    local aheadVehNameText = GetLabelText(GetDisplayNameFromVehicleModel(aheadVehHash))
					local aheadVehNumberPlate = GetVehicleNumberPlateText(GetVehiclePedIsUsing(GetPlayerPed(-1)))
                    local score = {}
					local datetime = GetLocalTime()

					score.player = aheadVehNumberPlate
                    score.time = finishTime
                    score.car = aheadVehNameText
					                    
                    -- Send server event with score and message, move this to server eventually

                    -- message = string.format("Player " .. GetPlayerName(PlayerId()) .. " finished " .. race.title .. " using " .. aheadVehNameText .. " in " .. (finishTime / 1000) .. " s")
					message = string.format("Player " .. score.player .. " finished " .. race.title .. " using " .. aheadVehNameText .. " in " .. (finishTime / 1000) .. " s" .. " on " .. datetime)
					-- Edit ML
					MySQL.Async.fetchAll("INSERT INTO racestatistics (FivemID, User, Track, Car, Date, Time) VALUES(@source, @name, @track, @car, @date, @time)",
					{["@source"] = GetPlayerName(PlayerId()), ["@name"] = score.player, ["@track"]= race.title, ["@car"] = aheadVehNameText, ["@date"] = datetime, ["@time"] = (finishTime / 1000)},
					-- Stop Edit ML
                    function(result)
					TriggerServerEvent('racePlayerFinished', GetPlayerName(PlayerId()), message, race.title, score)
					end)
                    -- Clear racing index and break
                    raceState.index = 0
                    break
                end

Thank you in advance for any tips

Edit 1: resource script for timetrials
client_scripts {
“tracks.lua”,
“timetrials_cl.lua”
}

server_scripts {
“@mysql-async/lib/MySQL.lua”,
“timetrials_sv.lua”

}

I am able to send data to the database as done in this tutorial of jeva so I know it’s not related to the SQL side (I assume)

tried adding a dependency to the resource script?

dependency 'mysql-async'

Tell me that isnt a client side snippet.

Thank you for takin the time to give feedback.

I adapted the resource file
client_scripts {
	"tracks.lua",
	"timetrials_cl.lua"
}

server_scripts {
	"@mysql-async/lib/MySQL.lua",
	"timetrials_sv.lua"

}

dependency ‘mysql-async’

Unfortunately, the same error pops up still attempt to index a nil value (global ‘MySQL’)

Kind regards,

I assume you say this because of the ability to access the SQL by putting it in client side? Unfortunately, I have not learned yet of another way to do this.

Thank you for your concern.

Send the data you need to the server side code, then the server side sends it to the DB, A client cannot directly access commands involving server side, Its NIll becasue its on server only???

MySQL.Async.fetchAll("INSERT INTO racestatistics (FivemID, User, Track, Car, Date, Time) VALUES(@source, @name, @track, @car, @date, @time)",
{["@source"] = GetPlayerName(PlayerId()), ["@name"] = score.player, ["@track"]= race.title, ["@car"] = aheadVehNameText, ["@date"] = datetime, ["@time"] = (finishTime / 1000)},
-- Stop Edit ML

this is wrong too

Ok, so just recap.
I make a text out via client side script of the data, then import the data via a server side script.
I will try and convert it that way. Many thanks.

Euh… ok… can you tell me then what it could be?

You can send it in an event as raw data, text, number, table, what ever you prefer, it just cant work from 100% client side as the sql resource was built for server side only.

https://brouznouf.github.io/fivem-mysql-async/

Look for the async.execute method for inserts.

Thank you for pointing me in the right direction. Without you, I would have lost x more days before finding this issue.

The solution works now.

In the client script, the necessary data is gathered

               -- Get vehicle name and create score
                local aheadVehHash = GetEntityModel(GetVehiclePedIsUsing(GetPlayerPed(-1)))
                local aheadVehNameText = GetLabelText(GetDisplayNameFromVehicleModel(aheadVehHash))
				local aheadVehNumberPlate = GetVehicleNumberPlateText(GetVehiclePedIsUsing(GetPlayerPed(-1)))
                local score = {}
				local player = GetPlayerName(PlayerId())

				score.player = aheadVehNumberPlate
                score.time = finishTime
                score.car = aheadVehNameText

And in the server script, I send it to the database.

RegisterServerEvent(‘racePlayerFinished’)
AddEventHandler(‘racePlayerFinished’, function(source, player, message, carid, title, score, newScore)
– Get top car score for this race
local msgAppend = “”
local msgSource = source
local msgColor = color_finish
local allScores = getScores()
local raceScores = allScores[title]
local datetime = os.date(‘%Y-%m-%d %H:%M:%S’)

if raceScores ~= nil then
  
  -- ML start: send data to SQLite3
  MySQL.Async.fetchAll("INSERT INTO racestatistics (FivemID, User, Track, Car, Date, Time) VALUES(@source, @name, @track, @car, @date, @time)",
  {["@source"] = source, ["@name"] = player, ["@track"]= title, ["@car"] = carid, ["@date"] = datetime, ["@time"] = score},
  function(result)
    --    TriggerClientEvent()
  end)
  -- ML End: continue with writing score in game
1 Like