Jump to content

compare number with nil


Eshtiz

Recommended Posts

It gives an error on line 261: attempt to compare number with nil

whole command stopped working

line: 16 here (only posted function)

function getNearbyVehicles(thePlayer, commandName) 
    if (exports.global:isPlayerAdmin(thePlayer)) then 
        outputChatBox("Nearby Vehicles:", thePlayer, 255, 126, 0) 
        local count = 0 
         
        for index, nearbyVehicle in ipairs( exports.global:getNearbyElements(thePlayer, "vehicle") ) do 
            local thisvehid = getElementData(nearbyVehicle, "dbid") 
            local vehicleID = getElementModel(nearbyVehicle) 
            local vehicleName = getVehicleNameFromModel(vehicleID) 
            local owner = getElementData(nearbyVehicle, "owner") 
            local faction = getElementData(nearbyVehicle, "faction") 
            count = count + 1 
             
            local ownerName = "" 
             
            if (faction>0) then 
                local theTeam = exports.pool:getElement("team", faction) 
                if theTeam then 
                    ownerName = getTeamName(theTeam) 
                end 
            elseif (owner==-1) then 
                ownerName = "Admin" 
            elseif (owner>0) then 
                ownerName = exports['cache']:getCharacterName(owner) 
            else 
                ownerName = "Civilian" 
            end 
             
            if (thisvehid) then 
                outputChatBox("   " .. vehicleName .. " (" .. vehicleID ..") with ID: " .. thisvehid .. ". Owner: " .. ownerName, thePlayer, 255, 126, 0) 
            elseif not (thisvehid) then 
                outputChatBox("   " ..  "*Temporary* " .. vehicleName .. " (" .. vehicleID ..") with ID: " .. thisvehid .. ". Owner: " .. ownerName, thePlayer, 255, 126, 0) 
            end 
        end 
         
        if (count==0) then 
            outputChatBox("   None.", thePlayer, 255, 126, 0) 
        end 
    end 
end 
addCommandHandler("nearbyvehicles", getNearbyVehicles, false, false) 

Link to comment

try this

function getNearbyVehicles(thePlayer, commandName) 
    if (exports.global:isPlayerAdmin(thePlayer)) then 
        outputChatBox("Nearby Vehicles:", thePlayer, 255, 126, 0) 
        local count = 0 
        
        for index, nearbyVehicle in ipairs( exports.global:getNearbyElements(thePlayer, "vehicle") ) do 
            local thisvehid = getElementData(nearbyVehicle, "dbid") 
            local vehicleID = getElementModel(nearbyVehicle) 
            local vehicleName = getVehicleNameFromModel(vehicleID) 
            local owner = getElementData(nearbyVehicle, "owner") 
            local faction = getElementData(nearbyVehicle, "faction") 
            count = count + 1 
            
            local ownerName = "" 
            
            if (tonumber(faction) > 0) then 
                local theTeam = exports.pool:getElement("team", faction) 
                if theTeam then 
                    ownerName = getTeamName(theTeam) 
                end 
            elseif (owner==-1) then 
                ownerName = "Admin" 
            elseif (owner>0) then 
                ownerName = exports['cache']:getCharacterName(owner) 
            else 
                ownerName = "Civilian" 
            end 
            
            if (thisvehid) then 
                outputChatBox("   " .. vehicleName .. " (" .. vehicleID ..") with ID: " .. thisvehid .. ". Owner: " .. ownerName, thePlayer, 255, 126, 0) 
            elseif not (thisvehid) then 
                outputChatBox("   " ..  "*Temporary* " .. vehicleName .. " (" .. vehicleID ..") with ID: " .. thisvehid .. ". Owner: " .. ownerName, thePlayer, 255, 126, 0) 
            end 
        end 
        
        if (count==0) then 
            outputChatBox("   None.", thePlayer, 255, 126, 0) 
        end 
    end 
end 
addCommandHandler("nearbyvehicles", getNearbyVehicles, false, false) 

Link to comment
try this
function getNearbyVehicles(thePlayer, commandName) 
    if (exports.global:isPlayerAdmin(thePlayer)) then 
        outputChatBox("Nearby Vehicles:", thePlayer, 255, 126, 0) 
        local count = 0 
        
        for index, nearbyVehicle in ipairs( exports.global:getNearbyElements(thePlayer, "vehicle") ) do 
            local thisvehid = getElementData(nearbyVehicle, "dbid") 
            local vehicleID = getElementModel(nearbyVehicle) 
            local vehicleName = getVehicleNameFromModel(vehicleID) 
            local owner = getElementData(nearbyVehicle, "owner") 
            local faction = getElementData(nearbyVehicle, "faction") 
            count = count + 1 
            
            local ownerName = "" 
            
            if (tonumber(faction) > 0) then 
                local theTeam = exports.pool:getElement("team", faction) 
                if theTeam then 
                    ownerName = getTeamName(theTeam) 
                end 
            elseif (owner==-1) then 
                ownerName = "Admin" 
            elseif (owner>0) then 
                ownerName = exports['cache']:getCharacterName(owner) 
            else 
                ownerName = "Civilian" 
            end 
            
            if (thisvehid) then 
                outputChatBox("   " .. vehicleName .. " (" .. vehicleID ..") with ID: " .. thisvehid .. ". Owner: " .. ownerName, thePlayer, 255, 126, 0) 
            elseif not (thisvehid) then 
                outputChatBox("   " ..  "*Temporary* " .. vehicleName .. " (" .. vehicleID ..") with ID: " .. thisvehid .. ". Owner: " .. ownerName, thePlayer, 255, 126, 0) 
            end 
        end 
        
        if (count==0) then 
            outputChatBox("   None.", thePlayer, 255, 126, 0) 
        end 
    end 
end 
addCommandHandler("nearbyvehicles", getNearbyVehicles, false, false) 

Thank you :D but what did you do? I have similar errors I'm trying to fix :?

EDIT:nvm

Link to comment
try this
function getNearbyVehicles(thePlayer, commandName) 
    if (exports.global:isPlayerAdmin(thePlayer)) then 
        outputChatBox("Nearby Vehicles:", thePlayer, 255, 126, 0) 
        local count = 0 
        
        for index, nearbyVehicle in ipairs( exports.global:getNearbyElements(thePlayer, "vehicle") ) do 
            local thisvehid = getElementData(nearbyVehicle, "dbid") 
            local vehicleID = getElementModel(nearbyVehicle) 
            local vehicleName = getVehicleNameFromModel(vehicleID) 
            local owner = getElementData(nearbyVehicle, "owner") 
            local faction = getElementData(nearbyVehicle, "faction") 
            count = count + 1 
            
            local ownerName = "" 
            
            if (tonumber(faction) > 0) then 
                local theTeam = exports.pool:getElement("team", faction) 
                if theTeam then 
                    ownerName = getTeamName(theTeam) 
                end 
            elseif (owner==-1) then 
                ownerName = "Admin" 
            elseif (owner>0) then 
                ownerName = exports['cache']:getCharacterName(owner) 
            else 
                ownerName = "Civilian" 
            end 
            
            if (thisvehid) then 
                outputChatBox("   " .. vehicleName .. " (" .. vehicleID ..") with ID: " .. thisvehid .. ". Owner: " .. ownerName, thePlayer, 255, 126, 0) 
            elseif not (thisvehid) then 
                outputChatBox("   " ..  "*Temporary* " .. vehicleName .. " (" .. vehicleID ..") with ID: " .. thisvehid .. ". Owner: " .. ownerName, thePlayer, 255, 126, 0) 
            end 
        end 
        
        if (count==0) then 
            outputChatBox("   None.", thePlayer, 255, 126, 0) 
        end 
    end 
end 
addCommandHandler("nearbyvehicles", getNearbyVehicles, false, false) 

Thank you but what did you do? I have similar errors I'm trying to fix

EDIT:nvm

if (tonumber(faction) > 0) then 

You have to convert variable faction to number. Then you can compare it with another number. You can't compare strings with numbers.

Now you know what he did?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...