Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Sting Value to Vector3 Value?

So I am saving a Vector3 (position x, y, z) eg 1, 1, 1 and because you can't save Vector3 values I put them into string values, saving/loading works etc but how do I set the position of it, I tried this:

Part.Position = Vector3.new(PlayerData.Position.Value)
Part.Parent = game.Workspace

didn't work, puts the part to 0, 0, 0 and I can't think of any other solutions.

String Value is in this format:

1, 1, 1

1 comment

0

To change a string to a number you can use the tonumber() function built into Lua.

2 answers

✓ Accepted answer

What you need is string patterns. They're a way to pull out certain information from a string using certain string functions in Lua and patterns that the language recognizes. To get a more detailed understanding read here:

http://wiki.roblox.com/index.php?title=String_pattern

Using string patterns there are a few ways to transform a string like you suggested, "x, y, z" into a vector 3 form. I'll go over one way, but just like normal programming there are many ways to approach the same problem.

-- an example string
local example = "1.76, 42, 0.53";
-- now we'll use gmatch to get the numbers:
local coord = {};
for num in string.gmatch(example, "[%d%.]+") do
	-- travels from left to right, so x, then y, then z
	table.insert(coord, tonumber(num));
end;
-- finally put into vector3 form
local v3 = Vector3.new(unpack(coord));
print(v3);

3 comments

0

OH MY GOD, THANK YOU SO MUCH

0

(yes, this is an old answer but if somebody is reading this from the future, add a - right before %d to include negative numbers)

0

^ EVEN BETTER THANK YOu

Hello, from what I have read, the problem is that you are trying to use a StringValue as a Vector3Value. Even though you have included the x,y,z coordinates into a StringValue, the value of a StringValue will always output as a string.

A way to fix this is to store the position (x,y,z) in a Vector3Value.

If you do not already know how to insert a Vector3Value, read the following: Go into studio>Right click>InsertObject>Vector3Value

2 comments

0

I don't think that's his problem.

0

Read again, you cannot store Vector3Value's which is why I am using stings instead.

This discussion is preserved as it was. New answers, comments, and votes are closed.