# Random Vector

Hey all,

This is very simple problem but I cant seem to figure it out, all I want it to do is use a random spawnpoint from the vector points I collected in the table.

[lua]

local citizenspawn =
{
“4524.13, 2885.91, 458.13”,
“-552.53, -3.28, 274.13”,

}

function playerRespawn( ply )

``````ply:SetPos(Vector(math.random(1, #citizenspawn)));
``````

end

[/lua]

Any help is much appreciated.

[editline]07:12PM[/editline]

Anyone got any ideas, I still have no luck on it.

Make sure you know what your writing, and why you’re writing it. If you don’t get past that point, you’re going no where. So I’m going to give you precise details about why your code is horrible, and how to fix it.

First of all:

[lua]ply:SetPos(Vector(math.random(1, #citizenspawn)))[/lua]

math.random(1, #citizenspawn) will return you a number between 1 and the size of citizenspawn. Since the size of your current table is 2, it could either mean ply:SetPos(Vector(1))) or ply:SetPos(Vector(2))).
Not making much sense, right?

What you would it to do is access one of the elements of the table chosen randomly, I hope you know how to access an element inside of a table, so your code should look like this now:

[lua]ply:SetPos(Vector(citizenspawn[math.random(1, #citizenspawn)]))[/lua]

Now look at the two different possibilities, that could either mean this:
[lua]ply:SetPos(Vector(“4524.13, 2885.91, 458.13”))[/lua]

or this:
[lua]ply:SetPos(Vector("-552.53, -3.28, 274.13"))[/lua]

This still does not make sense, because Vector takes 3 numerical values, and not strings. Anything between double quotes or quotes are literal strings that have about as much mathematical value as “Hello world”.
Your best bet is to store the positions directly as vectors inside of your table. Like this:

[lua]local citizenspawn =
{
Vector(4524.13, 2885.91, 458.13),
Vector(-552.53, -3.28, 274.13),
}[/lua]

This way, all you have to do after that is:

[lua]ply:SetPos(citizenspawn[math.random(1, #citizenspawn)])[/lua]

Also, since you seem to be a Lua beginner, please don’t have bad habits such as adding semicolons at the end of every line, this is Lua, not C. Although semicolons work, they are completely useless, a line of code with or without a semicolon will do exactly the same things.
Additionally, Lua comments are – and --[], and not // and /* */. Logical operators in Lua are “not”, “and” and “or”, and definitely not “!”, “&&” and “||”. There is absolutely no interest in writing “if(a==b)” instead of “if a==b”, unless you honestly think it is easier to read.

If you see those things in other people’s code, ignore them and do not imitate them, unless you really think writing like that makes things clearer.

Wrote a little more than I should, but hope that helped!

Just a random question here, do the operators really matter? I find Garry’s to be a hell of a lot cleaner looking than the native Lua ones, and they both do the exact same thing. I used to always use the Lua ones before I actually knew how to code, but I just found Garry’s to be space-saving and less of a clusterfuck after time passed.

and I always parenthesis my conditionals just to make sure there’s no possibility of them fucking up anywhere, especially when I’m doing multiple ones at the same time.

That was very helpful Killburn, I tried what you said and it worked perfect I will take into note what you said to me about the comments aswell

Well, if you think it’s easier to read with C style operators, then good for you. I personally prefer the good old Lua ones, although they are a bit longer. Plus, other Lua implementations don’t have C style operators, so if you’re used to writing && and ||, you might be a bit confused about why your script isn’t working.

I had that exact problem while implementing Lua into my application, once I realized what the problem was I felt so stupid. From then on I always use the Lua operators and not the C style ones.

I tend towards using the standard Lua ones as they make for more readable code in my experience. It would also mean I could use a standard lua editor and still get syntax highlighting.