|
| 19 Aug 2012 05:07 PM |
What's the best way to convert a string composed of say...1,1,1 into a Vector3 value?
~Death to lazy builders. Remove the CFrame tools. Command bar all the way. -pauljkl |
|
|
| Report Abuse |
|
|
josh50000
|
  |
| Joined: 29 Nov 2009 |
| Total Posts: 697 |
|
|
| 19 Aug 2012 05:17 PM |
| tonumber(*string here*) might work :/ |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:20 PM |
A string like "1, 1, 1"?
string = "1, 1, 1" position = Vector3.new(loadstring(string)())
if that doesn't work then try:
string = "1, 1, 1" loadstring("position = Vector3.new("..string..")")() |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:24 PM |
Neither one works for what I'm attempting to do. I'm attempting to get the string value from a textbox, then convert it to a Vector3 value, and apply it to a SpecialMesh's Scale property.
~Death to lazy builders. Remove the CFrame tools. Command bar all the way. -pauljkl Derpity derp derp. My favorite saying. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:30 PM |
This:
string = "1, 1, 1" loadstring("position = Vector3.new("..string..")")()
Didn't work? |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:31 PM |
| I just tried it. It worked for me... |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:33 PM |
t = {} stringv = "123,45,6" for s in string.gmatch(stringv, "%d+") do table.insert(t, tonumber(s)) print(s) end
a = Vector3.new(t[1],t[2],t[3]) |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:34 PM |
string = "1, 1, 1" loadstring("size = Vector3.new("..string..")")() game.Workspace.THE_PART_NAME.Mesh.Scale = size |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:35 PM |
| @awsumpwner: that would work too, but in this case, loadstring()() would be more efficient. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:36 PM |
It did...I wasn't using it correctly for my script. Trying not to shred the entire script to try to integrate that.
~Death to lazy builders. Remove the CFrame tools. Command bar all the way. -pauljkl Derpity derp derp. My favorite saying. |
|
|
| Report Abuse |
|
|
mamaguy
|
  |
| Joined: 07 Oct 2010 |
| Total Posts: 7073 |
|
|
| 19 Aug 2012 05:38 PM |
Ummmmm..... String = stringhere v3 = Instance.new("Vector3Value", String.Parent) v3.Value = String.Value part.Position = v3.Value |
|
|
| Report Abuse |
|
|
mamaguy
|
  |
| Joined: 07 Oct 2010 |
| Total Posts: 7073 |
|
|
| 19 Aug 2012 05:39 PM |
part.Position = Vector3.new(v3.Value) Correction^ |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:40 PM |
Math, your code works, but when I try to use it to get a variable that references a string it fails, with bad argument #1 to '?' (Vector3 expected, got userdata).
~Death to lazy builders. Remove the CFrame tools. Command bar all the way. -pauljkl |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:40 PM |
| Yeah, I checked, Mathman314's method is faster. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:42 PM |
Oh yeah, and incase you accidently don't have enough numbers, or forget a comma or something, use:
Put this in a script under the the TextBox:
script.Parent.Changed:connect(function() local succ = pcall(function() loadstring("size = Vector3.new("..script.Parent.Text..")")() end) if not size or not succ then size = Vector3.new(1, 1, 1) end game.Workspace.THE_PART_NAME.Mesh.Scale = size end)
That should take care of it for you (: |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:44 PM |
That's because you were using it wrong. Based on the error message, I assume you did something like:
Vector3.new(size)
where size was already converted to a Vector3 value for you (: |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:50 PM |
string = "1, 1, 1" a1=tick() loadstring("size = Vector3.new("..string..")")() a2=tick()
print("Mathman314: "..a2-a1)
t = {} stringv = "123,45,6" a1 = tick() for s in string.gmatch(stringv, "%d+") do table.insert(t, tonumber(s)) end a2 = tick() a = Vector3.new(t[1],t[2],t[3])
print("Me: "..a2-a1)
I just ran this script, and it would appear that my method actually runs faster. I think that code interpretation is more costly and takes a bit longer than string manipulation. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:52 PM |
Got it working. What I was doing was attempting to use an existing string value instead of the actual text value. Thanks!
~Death to lazy builders. Remove the CFrame tools. Command bar all the way. -pauljkl Derpity derp derp. My favorite saying. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 05:58 PM |
| Yours might run a bit faster, but it won't catch errors if there aren't enough numbers, or is missing a comma, or the format is incorrect, etc. Especially if it's a larger string. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 06:04 PM |
| Huh, it doesn't error. No matter how the string is formatted, as long as the string contains a number, it won't error. I guess that's a plus for your version. |
|
|
| Report Abuse |
|
|
|
| 19 Aug 2012 06:20 PM |
| But if there's only one number. Or none at all... |
|
|
| Report Abuse |
|
|