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: Converting a string to a Vector3?

Previous Thread :: Next Thread 
ultralegomaster5096 is not online. ultralegomaster5096
Joined: 17 Jan 2011
Total Posts: 4351
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 is not online. josh50000
Joined: 29 Nov 2009
Total Posts: 697
19 Aug 2012 05:17 PM
tonumber(*string here*) might work :/
Report Abuse
Mathman314 is not online. Mathman314
Joined: 31 Jul 2009
Total Posts: 556
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
ultralegomaster5096 is not online. ultralegomaster5096
Joined: 17 Jan 2011
Total Posts: 4351
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
Mathman314 is not online. Mathman314
Joined: 31 Jul 2009
Total Posts: 556
19 Aug 2012 05:30 PM
This:

string = "1, 1, 1"
loadstring("position = Vector3.new("..string..")")()

Didn't work?
Report Abuse
Mathman314 is not online. Mathman314
Joined: 31 Jul 2009
Total Posts: 556
19 Aug 2012 05:31 PM
I just tried it. It worked for me...
Report Abuse
awsumpwner27 is not online. awsumpwner27
Joined: 03 Sep 2011
Total Posts: 4389
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
Mathman314 is not online. Mathman314
Joined: 31 Jul 2009
Total Posts: 556
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
Mathman314 is not online. Mathman314
Joined: 31 Jul 2009
Total Posts: 556
19 Aug 2012 05:35 PM
@awsumpwner: that would work too, but in this case, loadstring()() would be more efficient.
Report Abuse
ultralegomaster5096 is not online. ultralegomaster5096
Joined: 17 Jan 2011
Total Posts: 4351
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 is not online. 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 is not online. mamaguy
Joined: 07 Oct 2010
Total Posts: 7073
19 Aug 2012 05:39 PM
part.Position = Vector3.new(v3.Value)
Correction^
Report Abuse
ultralegomaster5096 is not online. ultralegomaster5096
Joined: 17 Jan 2011
Total Posts: 4351
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
awsumpwner27 is not online. awsumpwner27
Joined: 03 Sep 2011
Total Posts: 4389
19 Aug 2012 05:40 PM
Yeah, I checked, Mathman314's method is faster.
Report Abuse
Mathman314 is not online. Mathman314
Joined: 31 Jul 2009
Total Posts: 556
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
Mathman314 is not online. Mathman314
Joined: 31 Jul 2009
Total Posts: 556
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
awsumpwner27 is not online. awsumpwner27
Joined: 03 Sep 2011
Total Posts: 4389
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
ultralegomaster5096 is not online. ultralegomaster5096
Joined: 17 Jan 2011
Total Posts: 4351
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
Mathman314 is not online. Mathman314
Joined: 31 Jul 2009
Total Posts: 556
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
awsumpwner27 is not online. awsumpwner27
Joined: 03 Sep 2011
Total Posts: 4389
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
Mathman314 is not online. Mathman314
Joined: 31 Jul 2009
Total Posts: 556
19 Aug 2012 06:20 PM
But if there's only one number. Or none at all...
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