SQL Callback Help For Transferring Data From Server to Client

So I am trying to develop a script where when you do a workout it will save to the sql and call it when you first join so you have your perks (stamina, strength speed, etc.) I am stuck with this error I keep getting:

[ script:mf_gym] SCRIPT ERROR: @mf_gym/server/main.lua:43: attempt to index a nil value (field '?')
[ script:mf_gym] > ref (@mf_gym/server/main.lua:43)
[ script:mf_gym] > async void MySQLAsync.Operation<System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>>+<ExecuteAsync>d__7.MoveNext() (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<List<Dictionary<string, object>>> MySQLAsync.FetchAll.ReaderAsync(MySqlCommand command) (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<DbDataReader> MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(string commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<MySqlDataReader> MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<ResultSet> MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ citizen-server-impl] server thread hitch warning: timer interval of 212 milliseconds
[ script:mf_gym] SCRIPT ERROR: @mf_gym/server/main.lua:43: attempt to index a nil value (field '?')
[ script:mf_gym] > ref (@mf_gym/server/main.lua:43)
[ script:trains] XNL-Trains: Player temptedx with id 1 will be the host for the trains
rateLimiter_getinfo_burstrefreshrestartrestartrestartrestartrestartrestart 247restart m4restart mf_addonpedsrestart mf_addonpedsrestart mf_gymrestart mf_gymrestart mf_gymrestart mf_gym
[ citizen-server-impl] Stopping resource mf_gym
[ c-scripting-core] Creating script environments for mf_gym
[ citizen-server-impl] Started resource mf_gym
cfx> [ citizen-server-impl] server thread hitch warning: timer interval of 448 milliseconds
[ script:mysql-async] SCRIPT ERROR in reference call: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
[ script:mysql-async] Parameter name: index
[ script:mysql-async]
[ script:mysql-async] SCRIPT ERROR in reference call: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
[ script:mysql-async] Parameter name: index
[ script:mysql-async]
rateLimiter_getinfo_burstrefreshrestartrestartrestartrestartrestartrestart 247restart m4restart mf_addonpedsrestart mf_addonpedsrestart mf_gymrestart mf_gymrestart mf_gymrestart mf_gym
[ citizen-server-impl] Stopping resource mf_gym
[ c-scripting-core] Creating script environments for mf_gym
[ citizen-server-impl] Started resource mf_gym
cfx> [ script:mysql-async] SCRIPT ERROR in reference call: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
[ script:mysql-async] Parameter name: index
[ script:mysql-async]
[ script:trains] XNL-Trains: Player temptedx with id 1 has quit. Looking for another host for the trains...
[ script:trains] XNL-Trains: No host are available for the trains. Waiting for another player to join the server...
[ script:es_extended] [SAVED] temptedx
[script:DiscordWhitel] 'temptedx' with ID '594365032645656589' was granted access and passed the whitelist.
[ script:connectqueue] QUEUE: temptedx[steam:110000141802076] was placed 1/1 in queue
[ script:connectqueue] QUEUE: temptedx[steam:110000141802076] is loading into the server
[ script:mf_gym] SCRIPT ERROR: citizen:/scripting/lua/scheduler.lua:1042: bad argument #1 to 'table_unpack' (table expected, got nil)
[ script:mf_gym] > async void MySQLAsync.Operation<System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>>+<ExecuteAsync>d__7.MoveNext() (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<List<Dictionary<string, object>>> MySQLAsync.FetchAll.ReaderAsync(MySqlCommand command) (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<DbDataReader> MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(string commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<MySqlDataReader> MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<ResultSet> MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] SCRIPT ERROR: citizen:/scripting/lua/scheduler.lua:1042: bad argument #1 to 'table_unpack' (table expected, got nil)
[ script:mf_gym] > async void MySQLAsync.Operation<System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>>+<ExecuteAsync>d__7.MoveNext() (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<List<Dictionary<string, object>>> MySQLAsync.FetchAll.ReaderAsync(MySqlCommand command) (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<DbDataReader> MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(string commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<MySqlDataReader> MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<ResultSet> MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)

Here is my server side:

function getStatus(cb)
    local identifier = GetPlayerIdentifiers(source)[1]
    MySQL.Async.fetchAll('SELECT * FROM gym WHERE identifier = @identifier', { ['@identifier'] = identifier }, function(result)

    	local stamina = result[1].stamina
    	local speed = result[1].speed
    	local strength = result[1].strength
	
    	local data = {
        	stamina = stamina,
        	speed = speed,
        	strength = strength
    	}

    	cb(data)
    end)
end

-- The event to call it
RegisterNetEvent('mf_gym:getStatus')
AddEventHandler('mf_gym:getStatus', function(cb)
    getStatus(cb)
end)
-- The function that gets called when the player joins in the client
function checkStatus()
	TriggerServerEvent('mf_gym:getStatus', function(data)
		if data.stamina == 0 then
			Citizen.Wait(100)
		elseif data.stamina == 1 then
			staminafull = true
		elseif data.stamina > 0.6 then
			staminafull = true
		elseif data.stamina < 0.6 then
			Ctiizen.Wait(100)
		end
		
		if data.strength == 0 then
			Citizen.Wait(100)
		elseif data.strength == 1 then
			Citizen.Wait(1)
		end

		if data.speed == 0 then
			Citizen.Wait(100)
		elseif data.speed == 1 then
			speed = 1
		elseif data.speed == 2 then
			speed = 2
		elseif data.speed == 3 then
			speed = 3
		elseif data.speed == 4 then
			speed = 4
		elseif data.speed > 0.5 then
			speed = 1 
		elseif data.speed < 0.5 then
			Citizen.Wait(100)
		elseif data.speed > 1.5 then
			speed = 2
		elseif data.speed < 1.5 then
			speed = 1
		elseif data.speed > 2.5 then
			speed = 3
		elseif data.speed < 2.5 then
			speed = 2
		elseif data.speed > 3.5 then
			speed = 4 
		elseif data.speed < 3.5 then
			speed = 3
		end
	end)
end

I am not the best when it comes to sql and I read the documentation to mysql, and did it in the way I thought it would work. Thank for the help. By the way the insert functions work and inserts the data but the fetch won’t.

In the getStatus function, you aren’t passing the source in the args. So maybe the error it’s just as simple as this

function getStatus(source, cb)
........
end

RegisterNetEvent('mf_gym:getStatus')
AddEventHandler('mf_gym:getStatus', function(cb)
    getStatus(source, cb)
end)

Hope it works

Unfortunately it did not. I’m gonna try ESX.RegisterServerCallback I’ll let you know how it goes.

That didn’t work either :slightly_frowning_face:

I’m getting a different error this time. It’s this and a bunch of async stuff:

[ script:mf_gym] SCRIPT ERROR: @mf_gym/server/main.lua:44: attempt to index a nil value (field ‘?’)
[ script:mf_gym] > ref (@mf_gym/server/main.lua:44)
[ script:mf_gym] SCRIPT ERROR: @mf_gym/server/main.lua:44: attempt to index a nil value (field ‘?’)
[ script:mf_gym] > ref (@mf_gym/server/main.lua:44)
[ script:mf_gym] > async void MySQLAsync.Operation<System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>>+<ExecuteAsync>d__7.MoveNext() (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<List<Dictionary<string, object>>> MySQLAsync.FetchAll.ReaderAsync(MySqlCommand command) (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<DbDataReader> MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(string commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<MySqlDataReader> MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<ResultSet> MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] SCRIPT ERROR: @mf_gym/server/main.lua:54: attempt to call a number value (upvalue ‘cb’)
[ script:mf_gym] > ref (@mf_gym/server/main.lua:54)
[ script:mf_gym] > async void MySQLAsync.Operation<System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>>+<ExecuteAsync>d__7.MoveNext() (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<List<Dictionary<string, object>>> MySQLAsync.FetchAll.ReaderAsync(MySqlCommand command) (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<DbDataReader> MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(string commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<MySqlDataReader> MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<ResultSet> MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] SCRIPT ERROR: @mf_gym/server/main.lua:54: attempt to call a number value (upvalue ‘cb’)
[ script:mf_gym] > ref (@mf_gym/server/main.lua:54)
[ script:mf_gym] > async void MySQLAsync.Operation<System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>>+<ExecuteAsync>d__7.MoveNext() (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<List<Dictionary<string, object>>> MySQLAsync.FetchAll.ReaderAsync(MySqlCommand command) (@mysql-async/MySQLAsync.net.dll:0)
[ script:mf_gym] > async Task<DbDataReader> MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(string commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<MySqlDataReader> MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)
[ script:mf_gym] > async Task<ResultSet> MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) (@mysql-async/MySqlConnector.dll:0)