Hugo_Almeidowski Posted January 29, 2019 Share Posted January 29, 2019 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 Link to comment
Moderators IIYAMA Posted January 29, 2019 Moderators Share Posted January 29, 2019 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 1 Link to comment
Hugo_Almeidowski Posted January 29, 2019 Author Share Posted January 29, 2019 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! Link to comment
Sorata_Kanda Posted January 29, 2019 Share Posted January 29, 2019 (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 January 29, 2019 by Sorata_Kanda 1 Link to comment
Zorgman Posted January 29, 2019 Share Posted January 29, 2019 if not argument then -- Do something end ^ This could also mean that argument == false. Irrelevant difference for OP's specific use case, but still 1 Link to comment
Hugo_Almeidowski Posted January 29, 2019 Author Share Posted January 29, 2019 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? Link to comment
Zorgman Posted January 29, 2019 Share Posted January 29, 2019 (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 January 29, 2019 by Zorgman 1 Link to comment
Hugo_Almeidowski Posted January 29, 2019 Author Share Posted January 29, 2019 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 Link to comment
Mr.Loki Posted January 29, 2019 Share Posted January 29, 2019 Another simpler way is creating a ternary operator. For example: argument3 = tonumber(argument3) and argument3 or getElementDimension(player) -- what this does is if arg3 is a number use it else it sets arg3 as the player's dimension no need for else, if etc... 1 1 Link to comment
Scripting Moderators thisdp Posted January 29, 2019 Scripting Moderators Share Posted January 29, 2019 addCommandHandler("test",function(player,command,...) local args = {...} print(#args) --How many arguments are passed end) 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now