crouton04
|
  |
| Joined: 07 Jul 2010 |
| Total Posts: 4459 |
|
|
| 11 Dec 2012 04:31 PM |
This code is supposed to get num1, num2, and the sign. However, it doesn't..
script.Parent.MouseButton1Click:connect(function () print("Math") num1 = script.Parent.Parent.Math.Text:sub(1) num2 = script.Parent.Parent.Math.Text:sub(3) sign = script.Parent.Parent.Math.Text:sub(2) if sign == "+" then script.Parent.Parent.Math.Text = num1 + num2 end end)
Question, does sub remove the letter? Or remove things around letter? |
|
|
| Report Abuse |
|
|
|
| 11 Dec 2012 04:33 PM |
Use sub(1, 1), sub(2, 2), and sub(3, 3). When you supply one argument, it captures the substring from your index supplied all the way to the end of the string.
To be honest, string.match would be a better choice for this. Would you like to see how to use it here? |
|
|
| Report Abuse |
|
|
|
| 11 Dec 2012 04:33 PM |
sub obtains a substring.
For instance,
local str = "abcdefgh" print(str:sub(2,4)) -- bcd
The 2nd through 4th characters. |
|
|
| Report Abuse |
|
|
crouton04
|
  |
| Joined: 07 Jul 2010 |
| Total Posts: 4459 |
|
| |
|
crouton04
|
  |
| Joined: 07 Jul 2010 |
| Total Posts: 4459 |
|
| |
|
|
| 11 Dec 2012 04:41 PM |
script.Parent.MouseButton1Click:connect(function () print("Math") local num1, sign, num2 = script.Parent.Parent.Math.Text:match("(%d+)%s*(.)%s*(%d+)") num1, num2 = tonumber(num1) or 0, tonumber(num2) or 0 if sign == "+" then script.Parent.Parent.Math.Text = num1 + num2 end end)
%d+ captures all digits, with at least one required, takign the longest sequence of digits (0-9). . is any character. This will be your sign. %s is whitespace. * denotes optional. Since neither of the %s* are in parenthesis, we do not capture the whitespace. |
|
|
| Report Abuse |
|
|
crouton04
|
  |
| Joined: 07 Jul 2010 |
| Total Posts: 4459 |
|
|
| 11 Dec 2012 06:43 PM |
THANKS!!!
I only need help with one more thing.. How would I get the precentage of a number to another number?? Would it be..
100%200
>%50
or would I use math?
100/200
>>.50 |
|
|
| Report Abuse |
|
|