I have issues sending data back to the client. I believe it is because I am not capable of converting the table value into a string, but can’t find out how to do it so any help would be appeciated. I can get the values to be show on server console
Error I receive in server console InvokeNative: execution failed: Argument at index 1 was null.
SCRIPT ERROR: Execution of native 000000002f7a49e6 in script host failed.
-- Helper function for notifying players
function notifyPlayer(source, msg)
-- Add custom notification here (use chat by default)
TriggerClientEvent('chatMessage', source, "[StreetRaces]", {255, 0, 0}, msg)
end
That got rid of the error message in the console! Thank you!
It also sent the notify to the player, however no data is displayed apart from from the standard “StreetRaces” defined in the notifyplayer function.
This is because the result needs to be put in a string?
Edit: also why does it work if you put the source in player but not if you use source itself for the destination? Sorry for the beginner questions but am trying to understand :-/
Remove print when you assign it to the playerRaces variable.
As for the player variable, it is to do with function and variable scope. Try printing the player variable and source to the console within the mysql callback function to see the difference of output (might help you understand what’s happening)
Pf ok it’s getting difficult for me and to me it seems that the json decode is not the easiest way to go. I had some succes yesterday by doing this code. By success, I mean when I replaced the msg = msg … part by print(result[counter].race_name I had a nice one below the other in the console that seemed more “ok” to export to a client.
- ML start list recorded races
RegisterNetEvent("StreetRaces:listRaces_sv")
AddEventHandler("StreetRaces:listRaces_sv", function()
local playerRaces = {}
local player = source
local counter = 1
local msg = "Races: "
MySQL.ready(function()
MySQL.Async.fetchAll("SELECT race_name FROM race",{},
function(result)
while result[counter].race_name ~= nil do
msg = msg .. " " .. tostring(result[counter].race_name)
counter = counter + 1
end
notifyPlayer(player, msg)
end)
end)
end)
-- ML start list recorded races
RegisterNetEvent("StreetRaces:listRaces_sv")
AddEventHandler("StreetRaces:listRaces_sv", function()
local player = source
local counter = 1
local msg = "Races:"
MySQL.ready(function()
MySQL.Async.fetchAll("SELECT race_name FROM race",{},
function(result)
while result[counter].race_name ~= nil do
local check = result[counter].race_name
notifyPlayer(player, "Race:" .. check)
counter = counter + 1
end
end)
end)
end)
So above code works and I can see a list of all races on the client. Unfortunately, I was not able to work out how to send it in 1 string to the client.
I know it is asking a lot, but is anyone willing to just show me how to concat the values I receive from the SQL and put it in a single string?