Jump to content

Error when detecting a number


Recommended Posts

Hello MTA, I have been trying to create a fully functional vehicle spawn system but it seems at line 52 I get the following error:

ERROR: pf\s_main.lua:52: attempt to compare string with number 

I read somewhere that lua will convert numbers to strings and likewise.

Anyways, here is the script

function createVehicleCommand ( thePlayer, commandName, carName ) 
    local x, y, z = getElementPosition ( thePlayer ) 
    if tonumber(carName) ~= nil then 
        if carName < 400 then 
            outputChatBox("Invalid Vehicle", player, 255, 0, 0) 
        elseif carName > 611 then 
            outputChatBox("Invalid Vehicle", player, 255, 0, 0) 
        else 
            local carM = getVehicleName(carName) 
            createVehicle(carName, x+5, y, z) 
            outputChatBox("Your " .. getVehicleName(carM) .. " (#" .. carM .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) 
            vehicles.id = vehicles.id+1 
        end 
    else 
        local carModel = getVehicleModelFromName(carName) 
        if not carModel then 
            outputChatBox("That is not a valid car name") 
        else 
            createVehicle(carModel, x+5, y, z) 
            outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) 
            vehicles.id = vehicles.id+1 
        end 
    end 
end 
addCommandHandler("v", createVehicleCommand) 

Line 52:

if carName < 400 then 

Sorry if the script is slightly poorly put together, I was in a rush when trying to make this part of it.

Link to comment
local carName = tonumber ( carName ) 
if ( type ( carName ) == "number" ) then 
    if ( carName < 400 or carName > 611 ) then 
        outputChatBox ( "Invalid Vehicle", player, 255, 0, 0 ) 
    else 
        -- Your code 
    end 
end 

Link to comment
function createVehicleCommand ( thePlayer, commandName, carName ) 
    local x, y, z = getElementPosition ( thePlayer ) 
    if (type(carName) == "number") then 
        if carName < 400 then 
            outputChatBox("Invalid Vehicle", player, 255, 0, 0) 
        elseif carName > 611 then 
            outputChatBox("Invalid Vehicle", player, 255, 0, 0) 
        else 
            local carM = getVehicleName(carName) 
            createVehicle(carName, x+5, y, z) 
            outputChatBox("Your " .. getVehicleName(carM) .. " (#" .. carM .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) 
            vehicles.id = vehicles.id+1 
        end 
    else 
        local carModel = getVehicleModelFromName(carName) 
        if not carModel then 
            outputChatBox("That is not a valid car name") 
        else 
            createVehicle(carModel, x+5, y, z) 
            outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) 
            vehicles.id = vehicles.id+1 
        end 
    end 
end 
addCommandHandler("v", createVehicleCommand) 

Link to comment
function createVehicleCommand ( thePlayer, commandName, carName ) 
    local x, y, z = getElementPosition ( thePlayer ) 
    local carModel = tonumber ( carName ) 
    if ( type ( carModel ) == "number" ) then 
         if ( carModel < 400 or carModel > 611 ) then 
            outputChatBox ( "Invalid Vehicle", player, 255, 0, 0 ) 
        else 
            createVehicle ( carModel, x + 5, y, z ) 
            outputChatBox ( "Your " .. getVehicleName ( carModel ) .. " (#" .. carModel .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0 ) 
            vehicles.id = ( vehicles.id + 1 ) 
        end 
    else 
        local carModel = getVehicleModelFromName ( carName ) 
        if ( not carModel ) then 
            outputChatBox ( "That is not a valid car name", player, 255, 0, 0 ) 
        else 
            createVehicle ( carModel, x + 5, y, z ) 
            outputChatBox ( "Your " .. getVehicleName ( carModel ) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0 ) 
            vehicles.id = ( vehicles.id + 1 ) 
        end 
    end 
end 
addCommandHandler ( "v", createVehicleCommand ) 

Link to comment
function createVehicleCommand ( thePlayer, commandName, carName ) 
        local x, y, z = getElementPosition ( thePlayer ) 
        if (type(tonumber(carName)) == "number") then 
            if tonumber(carName) > 400 and tonumber(carName) < 611 then 
                outputChatBox("Invalid Vehicle", player, 255, 0, 0) 
            else 
                local id = getElementModel(carName) 
                createVehicle(carName, x+5, y, z) 
                outputChatBox("Your " .. getVehicleName(id) .. " (#" .. tonumber(carName) .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) 
                vehicles.id = vehicles.id+1 
            end 
        else 
            local carModel = getVehicleModelFromName(carName) 
            if not carModel then 
                outputChatBox("That is not a valid car name") 
            else 
                createVehicle(carModel, x+5, y, z) 
                outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) 
                vehicles.id = vehicles.id+1 
            end 
        end 
    end 
    addCommandHandler("v", createVehicleCommand) 
  

Edited by Guest
Link to comment
function createVehicleCommand ( thePlayer, commandName, carName ) 
        local x, y, z = getElementPosition ( thePlayer ) 
        if (type(carName) == "number") then 
            if tonumber(carName) > 400 and tonumber(carName) < 611 then 
                outputChatBox("Invalid Vehicle", player, 255, 0, 0) 
            else 
                local carM = getVehicleName(carName) 
                createVehicle(carName, x+5, y, z) 
                outputChatBox("Your " .. getVehicleName(carM) .. " (#" .. carM .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) 
                vehicles.id = vehicles.id+1 
            end 
        else 
            local carModel = getVehicleModelFromName(carName) 
            if not carModel then 
                outputChatBox("That is not a valid car name") 
            else 
                createVehicle(carModel, x+5, y, z) 
                outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) 
                vehicles.id = vehicles.id+1 
            end 
        end 
    end 
    addCommandHandler("v", createVehicleCommand) 
  

There is no use of checking if "carName" type is a number, because it's going to come as a string.

Link to comment

Oh don't judge! I'm sorta a fan boy of Halo, grew up off that game lol

Anyways, @Cadu, it does spawn the vehicle using either a number or name.

But on both parts I get:

WARNING: pf\s_main.lua:56: Bad argument @ `getVehicleName` 
WARNING: pf\s_main.lua:58: Bad argument @ `getVehicleName` 
ERROR: pf\s_main.lua:58: attempt to concatenate local `carM` (a boolean value) 

Link to comment

try this:

function createVehicleCommand ( thePlayer, commandName, carName ) 
        local x, y, z = getElementPosition ( thePlayer ) 
        if (type(tonumber(carName)) == "number") then 
            if tonumber(carName) > 400 and tonumber(carName) < 611 then 
                outputChatBox("Invalid Vehicle", player, 255, 0, 0) 
            else 
                local carM = getVehicleNameFromModel(carName) 
                createVehicle(carName, x+5, y, z) 
                outputChatBox("Your " .. carM .. " (#" .. carName .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) 
                vehicles.id = vehicles.id+1 
            end 
        else 
            local carModel = getVehicleModelFromName(carName) 
            if not carModel then 
                outputChatBox("That is not a valid car name") 
            else 
                createVehicle(carModel, x+5, y, z) 
                outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) 
                vehicles.id = vehicles.id+1 
            end 
        end 
    end 
    addCommandHandler("v", createVehicleCommand) 

Link to comment
function createVehicleCommand ( thePlayer, commandName, carName ) 
    local x, y, z = getElementPosition ( thePlayer ) 
    local carModel = tonumber ( carName ) 
    if ( type ( carModel ) == "number" ) then 
         if ( carModel < 400 or carModel > 611 ) then 
            outputChatBox ( "Invalid Vehicle", player, 255, 0, 0 ) 
        else 
            createVehicle ( carModel, x + 5, y, z ) 
            outputChatBox ( "Your " .. getVehicleNameFromModel ( carModel ) .. " (#" .. carModel .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0 ) 
            vehicles.id = ( vehicles.id + 1 ) 
        end 
    else 
        local carModel = getVehicleModelFromName ( carName ) 
        if ( not carModel ) then 
            outputChatBox ( "That is not a valid car name", player, 255, 0, 0 ) 
        else 
            createVehicle ( carModel, x + 5, y, z ) 
            outputChatBox ( "Your " .. getVehicleNameFromModel ( carModel ) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0 ) 
            vehicles.id = ( vehicles.id + 1 ) 
        end 
    end 
end 
addCommandHandler ( "v", createVehicleCommand ) 

Link to comment

ok, I tried something else, here:

function createVehicleCommand ( thePlayer, _, carName ) 
       local x, y, z = getElementPosition ( thePlayer ) 
    local carModel = getVehicleModelFromName(carName) 
    local carName = getVehicleNameFromModel(tonumber(carName)) 
    if carModel then 
        createVehicle(carModel, x+5, y, z) 
        outputChatBox("Your " .. getVehicleNameFromModel(carModel) .. " (#" .. carModel .. ") was spawned with an ID of " .. vehicles.id, thePlayer, 0, 255, 0) 
    elseif carName then 
        local carM = getVehicleModelFromName(carName) 
        createVehicle(carM, x+5, y, z) 
        outputChatBox("Your " .. carName .. " (#" .. carM .. ") was spawned with and ID of " .. vehicles.id, thePlayer, 0, 255, 0) 
    else 
        outputChatBox("That is not a valid car name/model",thePlayer) 
    end 
end 
    addCommandHandler("v", createVehicleCommand) 

Edited by Guest
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...