3

I am trying to make several chatcommands in my new server modification for Crysis Wars that requires numbers and strings. To get the player input, I do this:

local name, time, reason = string.match(chatMsg, "^!punish (.*) (%d+) (.*)");

For some reason, it's not working properly (gets the number value as a string). The number value is the second variable in the string.match. Am I doing something wrong here? I've also tried to find a solution to this, but have not found any. Perhaps I should be using a different method to get the number?

1
  • 1
    this could not work, as tonumber takes (up to) two arguments, and the second must be a number type, while string.match returns as many string values as captures in the pattern; Like I wrote in my answer, add the line. Commented May 28, 2013 at 8:29

1 Answer 1

7

It's actually quite simple, just add the following line after the line you posted in your question.

time = tonumber(time);

As mentioned in the comments below, you can achieve the same with

time = time + 0
Sign up to request clarification or add additional context in comments.

4 Comments

Lua does do automatic conversion if you do arithmetic on decimal strings. e.g. "42"+"5" gives (the number) 47.
Ouch, I stand corrected. It seems it does. Well the answer is still correct in that this will convert the string value to a number cough
Agree with @finnw : time = time + 0 would do the same conversion ;-)
@EgorSkriptunoff There is one difference though: "a"+0 causes an error while tonumber("a") just returns nil

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.