Jump to content

How to check if argument is empty?


Recommended Posts

Posted

I can't find anything on Google.

So, I made this command called /criarped that has three arguments.

If I don't put all the three arguments, it returns an error

I want it that if I don't put let's say the third argument, it will just get my dimension. (I can do this) I just don't know how to check if the argument is empty

  • Moderators
Posted
arg1, arg2, ...: Each word after command name in the original command is passed here in a seperate variable. If there is no value for an argument, its variable will contain nil. You can deal with a variable number of arguments using the vararg expression, as shown in Server Example 2 below.

https://wiki.multitheftauto.com/wiki/AddCommandHandler

 

if argument then -- check
  
end

 

@Hugo_Almeidowski

 

  • Like 1
Posted
5 minutes ago, IIYAMA said:

arg1, arg2, ...: Each word after command name in the original command is passed here in a seperate variable. If there is no value for an argument, its variable will contain nil. You can deal with a variable number of arguments using the vararg expression, as shown in Server Example 2 below.

https://wiki.multitheftauto.com/wiki/AddCommandHandler

 


if argument then -- check
  
end

 

@Hugo_Almeidowski

 

So, if argument = nil then

 

end

 

Right?

Thanks!

Posted (edited)
45 minutes ago, Hugo_Almeidowski said:

So, if argument = nil then

 

end

 

Right?

Thanks!

if argument then
  
end

represents

if argument ~= nil then
  
end

 

If you'd like to check if it's nil:

if not argument then
  -- Do something
end

-- or

if argument == nil then
  -- Do something
end

 

EDIT: It doesn't matter which of the two methods you choose. As far as I know, the common code style uses

if not element then
  -- Do something
end
-- etc.

 

Edited by Sorata_Kanda
  • Like 1
Posted
if not argument then
  -- Do something
end

^ This could also mean that argument == false. Irrelevant difference for OP's specific use case, but still :)

  • Like 1
Posted
8 minutes ago, Sorata_Kanda said:

if argument then
  
end

represents


if argument ~= nil then
  
end

 

If you'd like to check if it's nil:


if not argument then
  -- Do something
end

-- or

if argument == nil then
  -- Do something
end

 

EDIT: It doesn't matter which of the two methods you choose. As far as I know, the common code style uses


if not element then
  -- Do something
end
-- etc.

 

But for what I've been coding it seems that

if argument then



end

means that if argument == true (is valid) then

so what if I want to check if the argument is valid?

Posted (edited)
9 minutes ago, Hugo_Almeidowski said:

But for what I've been coding it seems that


if argument then



end

means that if argument == true (is valid) then

so what if I want to check if the argument is valid? 

It means that argument has a value assigned and that the value is different from false.

What you are looking for is:

if not argument or type(argument)~= "number" then
  argument = getPlayerDimension(player)
end

 

Edited by Zorgman
  • Like 1
Posted
6 minutes ago, Zorgman said:

It means that argument has a value assigned and that the value is different from false.

What you are looking for is:


if not argument or type(argument)~= "number" then
  argument = getPlayerDimension(player)
end

 

I've done it like this

if argument and argument2 and argument3 then
  
else if argument and argument2 then
    
else if argument then
      
else
      
end

 

  • Scripting Moderators
Posted
addCommandHandler("test",function(player,command,...)
    local args = {...}
    print(#args) --How many arguments are passed
end)

 

  • Like 1

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