Make math.random() return a different number each time

Alrighty, so I have this loop, that assigns every player to a position at the begin of a round, however, they spawn in the same place which can get boring. I would like to use math.random() or something of the similar nature to assign a player to spot each round but mix it up. The player’s can’t spawn in the same place, so how would I make it return a different number each time it is ran?

This is what I currently have:
for pos, ply in pairs(player.GetAll()) do



Remove the position from the table after each iteration (provided you have enough positions)

how is this related to math random?

By selection of an index from the table to use as a position for the spawning

I’d recommend rewriting PlayerSelectSpawn to do what you want.

[lua]function GM:PlayerSelectSpawn( Player )[/lua]

Basically, you need to return a Point entity ( info_mobile_spawn or whatever you want to call it, but it needs to exist in your entities folder ) with the position and angles set, this is where the player would spawn. In the same function above, you’d essentially find the entity, if it doesn’t exist create it.

Then go through a list of spawns on the map and pick one randomly if it meets a few conditions ( A player can’t be standing there for example ). An easy way is to create a sub-helper function where it will build a list of possible spawns, then return a random one. By building a list of spawns ( Not the most optimal way to do things ) I mean you’re going through the list of spawns on the map, ensuring they meet the conditions and then adding them to a list; that will be your possible spawn locations list.

Then, you’d return a random table value. Both of the examples below do the exact same thing.

[lua]return possibleLocations[ math.random( 1, table.Count( possibleLocations ) ) ];
return table.Random( possibleLocations );[/lua]

Now, if you choose not to change the PlayerSelectSpawn function ( which does everything behind the scenes when you call Player:Spawn( ) ) and want to just set the player to a random spot in a less elegant solution, you’d basically do the same thing, except you wouldn’t locate or return the info_mobile_spawn Point entity, but you’d call Player:SetPos( spawnPosition ) and Player:SetEyeAngles( spawnAngle ) when you’ve found a random location.

To make sure that players do not take the same spot, use table.remove while randomizing which table entry is chosen.

playerspawnposition = math.random(1,#spawnpositionstable)
playerspawn = spawnpositionstable[playerspawnposition]

However, as suggested, you should use PlayerSelectSpawn. I just thought I’d give you some better understanding of randomizing table entries. This example destroys one entry in the table for every time a player spawns, making it unusable the next round, so you’ll want to copy the table first using table.Copy, then destroy the copy instead of the original. The original table will also need as many entries as the maximum player count.

Huh. Didn’t realize that table.remove shifted the values above it, down. Thanks so much!