• Some Looping Help
    17 replies, posted
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?
Show the table please.
[QUOTE=brandonj4;36583653]Show the table please.[/QUOTE] [url]http://codepad.org/BEibTH7d[/url]
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) hook.Add("Think", "CheckSpheres", function() local radius = 100 //circle radius for k,v in pairs(spwpos) do //loop table local players = ents.FindInSphere(v,radius) 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!
[QUOTE=BradenFase;36585234]Not EXACTLY what I was looking for BUT definitely set me on the right track! Thanks again![/QUOTE] 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]
[QUOTE=Freze;36590476][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][/QUOTE] 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?
[QUOTE=Chessnut;36590559]The ents.FindInSphere will return a table with numbers as indexes, why not just use k instead?[/QUOTE] 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.
True, my bad.
[QUOTE=Chessnut;36590749]True, my bad.[/QUOTE] Although, I could make a cleaner beforehand. Then set so the players can't spawn anything until 3 seconds is up...
[QUOTE=BradenFase;36591415]Although, I could make a cleaner beforehand. Then set so the players can't spawn anything until 3 seconds is up...[/QUOTE] Did you happen to figure this all out?
[QUOTE=brandonj4;36599393]Did you happen to figure this all out?[/QUOTE] Still working on it. Unless you have an idea
[QUOTE=brandonj4;36599393]Did you happen to figure this all out?[/QUOTE] 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\table.lua:408] invalid order function for sorting
[QUOTE=BradenFase;36607134]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\table.lua:408] invalid order function for sorting[/QUOTE] Is this the same code as before? It seems as if you changed it and you're not even checking the spawn points anymore.
[QUOTE=brandonj4;36612644]Is this the same code as before? It seems as if you changed it and you're not even checking the spawn points anymore.[/QUOTE] No I'm not checking the spawn points at this moment. Here's the code: [url]http://codepad.org/xtINlsTd[/url]
Sorry, you need to Log In to post a reply to this thread.