# Some Looping Help

So I’m trying to create a script that randomly places a targeted player into a large box. There are 4 places that can be allowed to spawn. I want to loop through a table containing the vectors then store if someone has spawned there already. How would I go about doing this?

I would do something like this:
[lua]
local spwpos = {}
spwpos[1] = Vector(-2067.78125, 549.71875, 1021.6875) //north
spwpos[2] = Vector(-1071.375, 1546.0625, 1021.6875) //east
spwpos[3] = Vector(-75.4375, 549.875, 1021.6875)
spwpos[4] = Vector(-1071.65625, -446.03125, 1021.6875)

for k,v in pairs(spwpos) do //loop table
for k,v in pairs(players) do //loop for players
if (ValidEntity(v) and v:IsValid) and (v:IsPlayer()) then // check for a player
–code here
end
end
end
end)
[/lua]

Not sure if I got the right idea of what you were trying to do but there it is!

Not EXACTLY what I was looking for BUT definitely set me on the right track! Thanks again!

Good to know and glad to help!

How would I be able to count the amount of players in ents.FindInX?

[lua]
local amount=0
for k, v in pairs(ents.FindInX)
if v and v:IsValid() and v:IsPlayer() then
amount = amount + 1
end
end
[/lua]

I have that but instead it’s in a function.

[lua]
local iter = 0
for k,v in pairs(ismax) do
if v:IsPlayer() then
iter = iter + 1
if iter < 4 then
target_ply:SetPos(spwpos[snum] + Vector(0,0,25))
target_ply:SetEyeAngles( spweyes[snum] )
end
end
end
[/lua]
This will not allow me to teleport anyone up to spwpos[snum].

The ents.FindInSphere will return a table with numbers as indexes, why not just use k instead?

Because if there’s anything else in the FindInX it will change the numbers of the indexes right? I want the script to do nothing if there are already 4 players within the FindInBox.

Although, I could make a cleaner beforehand. Then set so the players can’t spawn anything until 3 seconds is up…

Did you happen to figure this all out?

Still working on it. Unless you have an idea

Hit a roadblock. Can’t seem to sort this table by Member. Here’s the code:

[lua]
local ismax = ents.FindInBox(Vector(68.627121, 1689.610840, 1021.427856), Vector(-2210.981689, -589.656311, 2161.493408))
target_ply:SetLocalVelocity( Vector( 0, 0, 0 ) )
table.SortByMember(ismax, “Player”)
for k,v in pairs(ismax) do
print (k, v)
[/lua]

Here’s the Error:
[@lua\includes\extensions able.lua:408] invalid order function for sorting

Is this the same code as before? It seems as if you changed it and you’re not even checking the spawn points anymore.

No I’m not checking the spawn points at this moment. Here’s the code: