im getting this error bad argument #1 to 'gsub' (string expected, got table) if…
- User
im getting this error bad argument #1 to 'gsub' (string expected, got table)
if (Config.saveOnlyESXOwnedVehicles) then QBCore.Functions.CreateCallback("AP:getVehicleOwnership", function(source, plate, model) local trimmedPlate = string.gsub(plate, "^%s*(.-)%s*$", "%1") local results = SqlExecute('SELECT plate FROM player_vehicles WHERE plate = @plate OR plate = @fakeplate', { ['@plate'] = plate, ['@fakeplate'] = fakeplate }) return #results > 0 end) - Assistant
The error is occurring because the function 'string.gsub' expects a string as its first argument, but it's receiving a table instead. The variable 'plate' or 'fakeplate' might be a table instead of a string. You should check the data type of these variables and make sure they are strings before passing them to the 'string.gsub' function.
- User
how to fix it ?
- Assistant
You can try updating the code like this:
if (Config.saveOnlyESXOwnedVehicles) then QBCore.Functions.CreateCallback("AP:getVehicleOwnership", function(source, plate, model) local plateString = "" if type(plate) == "table" then plateString = plate[1] else plateString = plate end local trimmedPlate = string.gsub(plateString, "^%s*(.-)%s*$", "%1") local fakeplateString = "" if type(fakeplate) == "table" then fakeplateString = fakeplate[1] else fakeplateString = fakeplate end local results = SqlExecute('SELECT plate FROM player_vehicles WHERE plate = @plate OR plate = @fakeplate', { ['@plate'] = plateString, ['@fakeplate'] = fakeplateString }) return #results > 0 end) endIn this updated code, we are checking if the variables 'plate' and 'fakeplate' are tables or strings. If they are tables, we are getting the first index value as the string value. We are then passing these string values to the 'string.gsub' function and the SQL query. This should help fix the issue.
- User
im getting this error Cannot index a funcref in plate[1]
- Assistant
The error "Cannot index a funcref" occurs when you try to apply an indexing operation on a function reference value.
In the updated code that I provided earlier, I assumed that the
plateandfakeplatevariables could potentially be tables, based on the error you provided earlier. Therefore, I added a check to see if the value is a table or not. However, it seems that this assumption was not correct, andplateandfakeplateare actually strings.You can fix the error by removing the table check on
plateandfakeplateand directly using the values as strings:if (Config.saveOnlyESXOwnedVehicles) then QBCore.Functions.CreateCallback("AP:getVehicleOwnership", function(source, plate, model) local trimmedPlate = string.gsub(plate, "^%s*(.-)%s*$", "%1") local fakeplateString = "" if type(fakeplate) == "table" then fakeplateString = fakeplate[1] else fakeplateString = fakeplate end local results = SqlExecute('SELECT plate FROM player_vehicles WHERE plate = @plate OR plate = @fakeplate', { ['@plate'] = trimmedPlate, ['@fakeplate'] = fakeplateString }) return #results > 0 end) endIn this updated code, I removed the check on
plateand directly used it as a string instring.gsub. I only kept the check onfakeplateto avoid any potential errors.This should fix the error you are seeing.