Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

im getting this error bad argument #1 to 'gsub' (string expected, got table) if…

  1. 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)
    
  2. 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.

  3. User

    how to fix it ?

  4. 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)
    end
    

    In 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.

  5. User

    im getting this error Cannot index a funcref in plate[1]

  6. 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 plate and fakeplate variables 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, and plate and fakeplate are actually strings.

    You can fix the error by removing the table check on plate and fakeplate and 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)
    end
    

    In this updated code, I removed the check on plate and directly used it as a string in string.gsub. I only kept the check on fakeplate to avoid any potential errors.

    This should fix the error you are seeing.