Checking if a player is behind a wall?

I was just looking around Gmod Lua Wiki and I wanted to design some sort of heatvision, This is not for some kind of Wallhack. My question is, Is there anyway to show all targets that are not behind a wall of a players vision?

Help much appreciated.

[lua]
local tr = util.GetPlayerTrace( LocalPlayer(), (ply:EyePos() - LocalPlayer():EyePos()):Normalize() )
if tr.Entity == ply then
– There’s a clear shot to the player
else
– Something’s blocking the trace
end[/lua]

**[Util.GetPlayerTrace

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Util.GetPlayerTrace)**

Thanks for the quick reply, Is ‘ply’ shared or only serverside, as it does come up with the error saying ply is a Nil value.

ply is nil, you need to get the references to the player objects yourself. In your case, you want to do this to all players, so you should loop through them.

[lua]for _, ply in ipairs(player.GetAll()) do
– Code
end[/lua]

Thanks MakeR Ill give it a try and will post back in a second

ply is the player entity that could be behind a wall. It’s not a function.

[editline]07:58PM[/editline]

I was 5 mins late?

Wat

lol Yeah Unfortunately 5 mins late :stuck_out_tongue:

Ive just tried the script, Got no error messages but players are not being set the material, here is the code:

[lua]

function Heatvision()
for _, ply in ipairs(player.GetAll()) do
local tr = util.GetPlayerTrace( LocalPlayer(), (ply:EyePos() - LocalPlayer():EyePos()):Normalize() )
if tr.Entity == ply then
tr.Entity:SetMaterial( “wall/living” )
else
– Something’s blocking the trace
end
end
end
hook.Add(“HUDPaint”, “blaeah”, Heatvision)

[/lua]

Does the material materials/wall/living.vmt exist?

Yeah all correctly placed.

I’m not particularly sure why you’re using HUDPaint - that might be the problem. Use “Think” instead (HUDPaint is run every frame and is generally used for drawing to the HUD, Think is run ever millisecond or so and is good for things like this. Or, you could use a timer).

I’ve also had problems in the past with using SetColor and SetMaterial on players clientside in the past, but who knows.

Are there actually players behind the wall in the same visleaf?
Also, first try just applying the material on all players (eg. only put ply:SetMaterial in the for-loop) and see if that’s the problem.

Think is also run every frame.

Ok Thanks for the replys guys :D, Ill give them a try ASAP, Just need to go out for a couple of hours, Ill post back here when Ive tested the ideas.