• Getting distance between players
    5 replies, posted
Hey, is there anyway to find all players within a certain range of one player rather than doing player.getAll() And checking the vector:Distance() between them?
[QUOTE=Jarva;42205802]Hey, is there anyway to find all players within a certain range of one player rather than doing player.getAll() And checking the vector:Distance() between them?[/QUOTE] You could ents.FindInSphere, but then you would need to filter the result for only players. What you have described is the ideal way IMO.
You can use something like this. [lua] local Player = FindMetaTable("Player") if !Player then return end function Player:GetPlayersWithinRange(range) local t = {} for k,v in ipairs(player.GetAll()) do if IsValid(v) and v != self and self:GetPos():Distance(v:GetPos()) <= range then table.insert(t, v) end end return t end [/lua] --Fixed a typo
use ents.FindInSphere and check if each found entity is a player or not.
[QUOTE=Robotboy655;42211070]use ents.FindInSphere and check if each found entity is a player or not.[/QUOTE] Shouldn't checking the distance of all entities be slower than checking the distance of a list of players from a table [player.GetAll()] [editline]16th September 2013[/editline] [QUOTE=brandonj4;42206254]v:GetPos():Distance(v:GetPos()) <= range[/QUOTE] And shouldn't this be v:GetPos():Distance(self:GetPos())
[QUOTE=Robotboy655;42211070]use ents.FindInSphere and check if each found entity is a player or not.[/QUOTE] This is probably your best bet. If you don't wanna do it that way then... [lua] function PlayersNearMe(me, range) local t = {} local mypos = me:LocalToWorld(me:OBBCenter()) for _, pl in pairs(player.GetAll()) do if me ~= pl and pl:NearestPoint(mypos):Distance(mypos) <= range then table.insert(t, pl) end end return t end [/lua]
Sorry, you need to Log In to post a reply to this thread.