Player is Within x Units of an Entity

Hey, is there a way of doing this?

I want a player to be able to use a SWEP when they are inside a designated area. how would I go about this?


(User was banned for this post ("Wrong forum" - mahalis))

if ply:GetPos():Distance(ent:GetPos())<X then

(Entity:GetPos() - Player:GetPos()):Length()

Many thanks…

Entity:GetPos():Distance(Player:GetPos())
:v:


local vec = ply:GetPos() - ent:GetPos()
local dist = math.sqrt(vec.x^2 + vec.y^2 + vec.z^2)

:v:

[lua]
local x,y,z = ply:GetPos() - ent:GetPos()
local dist = math.sqrt(x^2 + y^2 + z^2)
[/lua]
more efficient

That wouldn’t work, you would get an error saying “attempt to perform arithmetic on a table value” or something along those lines. ply:GetPos() - ent:GetPos() returns a table, not three values.

Right, Lua’s math functions are quicker than a call into the C++ vector distance stuff. Duh.