Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make a string a math formula?

Asked by 4 years ago

Hey there, alright so, I have been trying to do this for quite a while now but it isn't working out.

    local modifiers = {
["straightcurved2"] = -Target2Name.Size.X,
["straightcurved1"] = -Target2Name.Size.Z
}

So, is there a way to make the error of Target2Name not being there yet not exist? i.e. catching it or something, I've come so far that I turned

 ["straightcurved2"] = **-Target2Name.Size.X**,
 ["straightcurved1"] = **-Target2Name.Size.Z**

into a string to make the error go away, but I don't know how to get it back to how it was again.

2 answers

Log in to vote
0
Answered by 4 years ago

Protected call.

Lua has pcall, short for protected call. If you've used languages like C++, Python, ect you will know what try is. What pcall does is, receive a function as an argument, and call it in protected mode, so that runtime errors (errors that happen whilst your program is running) do not propagate and thus not terminate the program.

  1. pcall's first return value is always a boolean. true is returned if the function was called successfully without any runtime errors. false is returned if there was an error.
  2. Everything else after the first return value is what the function returns. If pcall returns false, the second return value is the error message.
  3. You can pass additional arguments and they will be the arguments to the function you have passed.

Example;

```lua local function add(a, b) return a + b; end

local ok, result = pcall(add, 1, 2); print(ok, result); --> true 3 ```

3 is the result of the add function and is returned by pcall.

Let's get an error.

```lua --// using the same add function local ok, result = pcall(add, 5, {}); --// you cannot perform arithmetic on a table value (without __add metamethod but that is beside the point)

print(ok, result); --> false input:1: attempt to perform arithmetic on a table value (local 'b') ```

The last value that was printed is the error message, and the first one is false because there was an error


You're better off waiting for Target2Name to exist via :WaitForChild though, but this answer is meant to answer this question of yours:

So, is there a way to make the error of Target2Name not being there yet not exist? i.e. catching it or something, I’ve come so far that I turned [...]

Ad
Log in to vote
-1
Answered by
mc3334 649 Moderation Voter
4 years ago

When answering, if your answer does not fully solve the question, it should be written as a comment to the question instead of as an answer.

hello! Im a bit unclear about your question, however I can tell you that there are two main functions to convert from numbers to strings and vice versa. Here is an example

--tostring() = This will take the string from a number.  For example:   print(tostring(5)) would be equal to print("5")
--tonumber() = This will take a number from a string.  For example:  TextLabel.Text = "5"; INTvalue.Value = tonumber(TextLabel.Text)

With further questions, please add a comment. Also, please try to be as specific and clear as you can in your questions. I could not understand this one so I just gave you the basic answer. With more information, I could have fully answered your question.

Answer this question