Range?

I’m trying to make a few lines that would detect if the player is within a certain range of a point. I tried searching the wiki for something that would help with this but couldn’t fine anything. Does anyone else know of anything?

thanks.

[lua]hook.Add(“Think”, “DetectRange”, function()
for _,v in pairs(ents.FindInSphere(Vector(0,0,0), 200)) do --ents.FindInSphere(vector of the point, radius)
if v:IsPlayer() then
–Write your code here
end
end
end[/lua]

Awesome! Thanks so much.

Uhm. You couldn’t have searched much. There’s a method for vectors called Distance(), which measures the distance between two vectors: Vector(0,0,10):Distance(Vector(0,0,0)) == 10.

I’d recommend the previous method though, since the distance method is entirely written in Lua while the other method uses a function that’s part of the game engine, which is MUCH faster.
However, with many entities it -might- be faster to use (vectorB - vectorA):LengthSqr(), which gives you the distance squared and is much faster than using the distance function. Adjusting the range to check for is easy, just multiply it by itself.