Jump to content

Error when detecting a number


Recommended Posts

Posted

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.

All kinds of feedback are welcome on all of my posts.

Posted

Command arguments will be always a string, you have to convert it to a number.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Right. How would I approach whilst keeping it able to detect if someone puts in a vehicle name? :roll: Sorry, I am not familiar with working with vehicles.

All kinds of feedback are welcome on all of my posts.

Posted
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 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Post your entire script.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
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) 

All kinds of feedback are welcome on all of my posts.

Posted
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 ) 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted (edited)
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

Ingame nick: Cadu12

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

My in-game name: Jaysds1

Retired CMG Scripter

World Of Tanks GameMode (Open-Source): https://github.com/Jaysds1/mtasa-wot-gamemode

Online GUI-Editor (WIP): https://forum.mtasa.com/topic/47678-online-gui-editor/

 

sE5Qm.png

TiV3C.png

img.php?id=0&text=Lua%20Scripter

Posted

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) 

All kinds of feedback are welcome on all of my posts.

Posted

Instead of keeping just with one answer, check mine as well, it should work.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

@Castillo, yours works but fails to display the line where it says "Your vehicle was spawned.."

@Cadu, same problems.

EDIT:

@jays, yes but I work like a million :D

All kinds of feedback are welcome on all of my posts.

Posted

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) 

My in-game name: Jaysds1

Retired CMG Scripter

World Of Tanks GameMode (Open-Source): https://github.com/Jaysds1/mtasa-wot-gamemode

Online GUI-Editor (WIP): https://forum.mtasa.com/topic/47678-online-gui-editor/

 

sE5Qm.png

TiV3C.png

img.php?id=0&text=Lua%20Scripter

Posted
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 ) 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted (edited)

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

My in-game name: Jaysds1

Retired CMG Scripter

World Of Tanks GameMode (Open-Source): https://github.com/Jaysds1/mtasa-wot-gamemode

Online GUI-Editor (WIP): https://forum.mtasa.com/topic/47678-online-gui-editor/

 

sE5Qm.png

TiV3C.png

img.php?id=0&text=Lua%20Scripter

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