generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: More string pattern help

Previous Thread :: Next Thread 
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
12 Aug 2012 10:47 PM
pattern = "(w%+)/(w%+)(/%)(w%+)(/%)(w%+)"
for command, arg1,slash1,arg2,slash2, arg3 in mesage:match(pattern) do

That is the part of my script that is broken.

The question I'm having is the for line breaks if slash1, arg2, etc are absent from the string and I'm curious on how to fix it

For example, one of my admin commands is ban/playername. Arg2,slash2, and arg3 are absent, therefore, it breaks.

Is there a way to fix this without using too many if lines?

I'm sorry if I'm not clear
Report Abuse
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
13 Aug 2012 12:58 AM
bump
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
13 Aug 2012 01:01 AM
Put a question mark (?) after any specifiers you want to be optional.

http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
13 Aug 2012 01:02 AM
That was simple. Thanks.
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
13 Aug 2012 01:03 AM
No problem. ;)

http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
13 Aug 2012 01:05 AM
Still the same error:

Workspace.Script:208: attempt to call a nil value

I don't want to post the script because it's too long :/

Here is the line:

pattern = "(w%+)/(w%+)(/%?)(w%+?)(/%?)(w%+?)"
Report Abuse
coolbob44 is not online. coolbob44
Joined: 26 Nov 2009
Total Posts: 1649
13 Aug 2012 01:11 AM
Use gmatch not match if you are going to use a loop
Report Abuse
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
13 Aug 2012 01:19 AM
Thanks, that fixed the error, but now I have a logic error!! dang it!!
Report Abuse
coolbob44 is not online. coolbob44
Joined: 26 Nov 2009
Total Posts: 1649
13 Aug 2012 01:21 AM
Give an example of the string you are trying to match. (e.g. "kill/coolbob44/with/fire"), and what you are using it for
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
13 Aug 2012 01:25 AM
You messed up the pattern a bit. Specifiers have a percentage sign in front of the letter, not after.

pattern = "(%w+)/(%w+)/(%w+?)/(%w+?)/(%w+?)/(%w+?)"

http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
13 Aug 2012 01:30 AM
kk, that's fixed. Another question: I have stored multiple functions in one table. To access those functions, can I do this?

table["derp"](arg1)

I know you can do table.derp(arg1) but I don't think that'll work with the current way I'm writing the script
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
13 Aug 2012 01:33 AM
You're asking if you can call a function in a table by calling a function? Like:

    local tbl = {
        ["FunctionA"] = function() print"1" end,
        ["FunctionB"] = function() print"2" end
    end}
    
    tbl("FunctionA")

?


http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
13 Aug 2012 01:36 AM
Like this:

Someone says ban/player

The function "admincommands" has a function called "ban"

it finds it by using the string pattern and "command" to do something like this:

adminc[command](player)

that should run the "ban" function within the adminc table, shouldn't it?
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
13 Aug 2012 01:38 AM
You'd better elaborate a little bit.

http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
13 Aug 2012 01:41 AM
adminc = {
["say"] =
function(argu)
print(argu)
end
}


pattern = "(w%+)/(w%+)(%/?)(%w+?)(%/?)(%w+?)"
for command, arg1,slash1,arg2,slash2, arg3 in mesage:gmatch(pattern) do
adminc[command](argu)


Now, when I say say/derp, will that run the function in the table?
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
13 Aug 2012 01:46 AM
No. I already fixed the pattern (I think), so just change it to the fixed version after.

adminc = {
["say"] =
function(argu)
print(argu)
end
}
pattern = "(w%+)/(w%+)(%/?)(%w+?)(%/?)(%w+?)"
for command, arg1,slash1,arg2,slash2, arg3 in mesage:gmatch(pattern) do
if command == "say" then
adminc["say"]()
end
end

http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
13 Aug 2012 01:47 AM
Wait, I mean:

adminc = {
["say"] =
function(argu)
print(argu)
end
}
pattern = "(w%+)/(w%+)(%/?)(%w+?)(%/?)(%w+?)"
for command, arg1,slash1,arg2,slash2, arg3 in mesage:gmatch(pattern) do
if command == "say" then
adminc.say()
end
end

http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
13 Aug 2012 01:48 AM
And you'd better put an argument in between the parenthesis.

http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
13 Aug 2012 01:53 AM
I understand. Thanks! Hopefully, one last question however:

if admincommands[command:lower()] then
print("Go ahead")

Even when the if line checks true, it doesnt' print "go ahead"

Report Abuse
Brofessions is not online. Brofessions
Joined: 30 Jul 2012
Total Posts: 214
13 Aug 2012 03:07 AM
Bump
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
13 Aug 2012 06:05 AM
Here's a generic arg parser that works for any number of arguments:

    local commands = {}
    function execute(cmd)
        -- separate the string at the first slash, leaving the leading / in argstring
        local name, argstring = cmd:match("^([^/]+)(.+)$")
        local args = {}

        -- iterate over each / followed by 0 or more non-slashes
        for arg in argstring:gmatch("/([^/]*)") do
            table.insert(args, arg)
        end
    
        commands[name](unpack(args))
    end

To use it:

    commands.kill = function(player) print("Killing " .. player) end
    commands.add = function(a, b) print(tonumber(a) + tonumber(b)) end
    commands.thirdargonly = function(_, _, test) print(test) end

    execute('kill/NXTBoy')

    execute('add/1/2')

    execute('thirdargonly/ignored/ignored/Third argument received')
    execute('thirdargonly///Third argument received') --passes "" to first two arguments
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
13 Aug 2012 01:18 PM
Oh, also, I got the pattern a bit wrong; I was tired. Use question marks for the optional slashes, and change the plus signs to asterisks for the optional fields. So:

    local pattern = "(%w+)/(%w+)/?(%w*)/?(%w*)/?(%w*)/?(%w*)"

http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image