I'm trying to pick a certain part of the map (a box) and spawn players randomly within that box. I'm lost and don't know what to do or if this is even possible. I posted my code below of what I've tried.
function GM:PlayerSelectSpawn(ply)
local spawns = ents.FindInBox( Vector("734.316040 9823.770508 -1236.968750"), Vector("3.527851 9297.077148 -1170.218750") )
local random_entry = math.random( #spawns )
return spawns[ random_entry ]
end
Looks fine to me. Only problem is your max vector has elements less than your min.
How should it look like then? I'm confused, I first picked the bottom left corner for the first vector and the top right corner for the second vector.
If you want to select a cube in a 3D space you need to have a minimum vector and a maximum vector, meaning all three values in the first one have to be less than the ones in the third one. The easiest way to explain it is that if you have Vector(0,0,0) as the first point and Vector(10,10,10) as the second, it will look through all entities found in between 0 and 10 of each axis.
Another thing is that to define vectors you need to separate them with commas like I did, you basically just put a string inside that vector which will return an error, you basically did: The three NUMBERS that represent the XYZ axis of this point I want is "banana", doesn't make a lot of sense for the program right?
Here, I fixed the numbers for you:
Vector(0, 9300, -1230), Vector(730, 9820, -1170)
Feel free to modify the numbers, I just rounded them up a bit to make them not look ugly.
Player still does not spawn in the cube. (As I stated above there are no entities in this cube I'm selecting, I'm trying to take out a random vector from inside of the box)
function GM:PlayerSelectSpawn(ply)
local spawns = ents.FindInBox( Vector(0, 9300, -1230), Vector(730, 9820, -1170) )
local random_entry = math.random( #spawns )
return spawns[ random_entry ]
end
Then don't use ents.FindInBox. Just do three random floats for a vector between the min and max arguments.
Problem is, the spawn points are made in the map maker (Hammer) and what you are trying to do is find them inside a box and then spawn the player inside one of the ones in there. If there are no spawn points in that box then you will get an error! I can see you were inspired by the example found in the gmod wikia for PlayerSelectSpawn but what that example code does is just find all the spawn points and select a random one for the player to spawn, it already does that.
What you can do instead is use the PlayerSpawn hook and set the position of the player to something like:
Vector(math.Rand(0,1000), math.Rand(0,500), math.Rand(0,20))
That will get a vector that is in between Vector(0,0,0) and Vector(1000,500,20) (Just like Code_GS said above there)
Thank you guys, math.Rand slipped my mind when I was trying to figure this out.
Sorry, you need to Log In to post a reply to this thread.