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

How do you make Roblox recognize Vector3 in a String?

So I'm trying to input Vector3 into a String to use Math.random to choose random positions, But I can't figure out how.

Here is my script.

local stringo = {58.834 167.946 175.373},{41.331 136.646 -37.613},{225.535 14.835 -277.032},{245.167 25.48 401.167},{-314.972 23.685 -241.663},{-300.111 24.372 386.007},{-211.486 117.346 162.886}

I hope someone can help me.

2 comments

0

You can just use math.random() in a Vector3. I think.

0

Yeah, but I'm choosing specific random locations.

2 answers

You should use Vector3s in your table instead, unless you're wanting to do something else with those values.

local positions = {
	Vector3.new(1,1,1),
	Vector3.new(2,2,2),
	Vector3.new(3,3,3),
}

local randomPosition = positions[math.random(#positions)]

By doing table[key] you access the value in a table whose index is key. For normal tables, ones without indexes the key is usually a number; hence we're use math.random().

The #positions, shows us how many elements there are in a table. By doing math.random we choose a random number from 1 to however many elements there are. And so, when we say positions[math.random(#positions)] we choose a random element.

If you really only want the position to be random then you could simply do this instead:

local position = Vector3.new(
	math.random(255),
	math.random(255),
	math.random(255),
)

Otherwise, if you need it to be randomized in specific a locations then put all of those values inside of a table and you'll be good to go:

local stringo = {
	{58.834, 167.946, 175.373},
	{41.331, 136.646, -37.613},
	{225.535, 14.835, -277.032},
	{245.167, 25.48, 401.167},
	{-314.972, 23.685, -241.663},
	{-300.111, 24.372, 386.007},
	{-211.486, 117.346, 162.886}
}

function GetRandomVector()
	local tableId = math.random(#stringo)
	local axis = stringo[tableId]

	return Vector3.new(axis[1], axis[2], axis[3])
end

local location = GetRandomVector()
print(location) --> some random vector from the [stringo] table

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