# If you're close to this NPC then allow else dont.

``````
Problem Solved

``````

How would I go on about, allowing a player to run a console command, only if he’s close to a specific NPC?

I’m guessing **[Ents.FindByName

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Ents.FindByName)** and distance <= 20 then … But how, exactly?

snip

[lua]
local npc = Ents.FindByName(“Your NPC name”)
if npc[1]:GetPos():Distance(ply:GetPos()) < yourDistance then
end
[/lua]
This is with the assumption that only one entity in your map has that name. You could also do ents.FindInSphere and then just checks the ents name

I may have messed something up in that code, if I did someone correct me.

ents.findinsphere would probably be more efficient.

It’s more efficient to sort out the entities with the wrong names before doing expensive distance checks.

[lua]
local distance = 20
local npc = ents.FindByName(“Your NPC name”)
if (npc[1]:GetPos() - ply:GetPos()):LengthSqr() < distance^2 then
end
[/lua]

You had ents with a capital E and for comparing distances don’t use :Distance()

Thanks guys, I’ll try it later

You mind if I ask what is wrong with distance, compared you what you are doing? Is distance really that much more expensive?

If you don’t use it often, like every frame, nothing. But the distance calculation uses square root which is a relatively slow function.

Ah that is good to know as I am using it quite a bit in my code. I guess I’ll be creating my own distance function then. Thanks.

This didnt work.

My current code:
local distance = 20
local npc = ents.FindByName(“crazy_pete”)
if (npc[1]:GetPos() - ply:GetPos()):LengthSqr() < distance^2 then – line 38
ply:Give(“weapon_crowbar”)
ply:SetHealth(ply:Health() + 50)
end
end

The error:
[lua\entities\crazy_pete\init.lua:38] attempt to index field ‘?’ (a nil value)

Still lost

My guess is that there are no entities by the name ‘crazy_pete’ thus causing npc[1] to be nil

Here is one I just wrote up. I’m sure it can be condensed but I didn’t care too much since it was just meant to show a point.

[lua]
local v1pos = v1:GetPos()
local v2pos = v2:GetPos()
local x = v1pos.x - v2pos.x
local y = v1pos.y - v2pos.y
local z = v1pos.z - v2pos.z
if (xx + yy + z*z) < (150^2) then
end
[/lua]

@S W

I’ll give it a go. Thanks for the effort

I actually took the time to test your code and as I suspected I didn’t get any errors when having an entity named ‘crazy_pete’ in the map. You might wanna double check the name because there’s nothing wrong with that code.

Still gives me the error, and honestly, S W - I have no idea what to do with that code.

Then you haven’t named the entity correctly.

All you have to do is put in your 2 entities for v1 and v2. Actually I ended up using a more condensed version of the function I wrote above in a gamemode I’m coding for. Here is the function I wrote.
You give it 3 parametersm Entity 1’s position, entity 2’s position, and the maximum distance that can be between the 2. If the 2 entities are further apart than distance it will return false; otherwise it will return true.
[lua]
function Distance(pos1, pos2, distance)
if ((pos1.x - pos2.x)^2 + (pos1.y - pos2.y)^2 + (pos1.z - pos2.z)^2) < (distance^2) then
return true
else
return false
end
end
[/lua]

That’s a bit… unneeded. You just recreated **[Vector.LengthSqr

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Vector.LengthSqr)**
So your solution is practically the same as mine few posts above.

Yes it is essentially the same, but OP said it didn’t work even though it should’ve.