Easier Way To Detect A Player Behind A Wall?

Haven’t it tested yet but it should work for experimental environments.

What does this exactly do? Detect when a players camera hits another player?

Basically it casts a ray from the camera no matter what radius it will always detect unless you change the Magnitude part and replace the entire line with just a number for the radius.

I realized something, with the way my camera is angled it doesn’t hit any part, it’s too zoomed out to hit a part. So I think there really isn’t anyway to answer my question since the way the camera is angled.

Nah you need other players in the game to test it only works on players in game but i can change that, just test in studio with 2 player server thing aka Emulator inside studio. It also ignores the (PLAYER whos casting) it this could lag because it will spam print.

So how would I make the script detect if it hits a part and makes the part go transparent?

this will help ??

local cam = game.Workspace.CurrentCamera
while task.wait() do 
local RayOrigin = cam.CFrame.Position
local RayDirection = cam.CFrame.LookVector * 1000
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}----here black list 
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(RayOrigin, RayDirection, raycastParams)
if raycastResult then
part = raycastResult.Instance	 ----- what it hits
end
end

So on the line

if raycastResult then
part = raycastResult.Instance

Would I put

part.Transparency = 0.5

?
Also, if I was to do that, how would I put the part’s transparency back to 0 if the camera leaves the part?

ye part.Transparency = 0.5 but idont know how to get it back to 0

Damn, that uh, sucks a lot lol.

ithink you can add cooldown that will get it back to 0 when cast leave

I tested out the script, and here’s what happens when I play tested.


It doesn’t appear to be working. I can give the script that has the camera stuff if that might help?

You can try using Invisicam.

What exactly does invisicam do?

Also, I tried the code snippet it provided and the part’s don’t go translucent.

This is probably the most efficient way:

  • Every frame, raycast from the camera to the player
  • If it hits a wall, make it transparent
  • If it doesn’t hit a wall, make it opaque

I understand that, but as I said even after reading the raycast api I don’t understand it. I think if it’s going to be this difficult, I should just stick to my primitive way.

I think the easiest way is to use the :GetTouchingParts() method. What people are wanting you to do is Raycast (which is shooting a line from a point in the world in a direction determined by you to see if it hits something) to draw a line between your camera’s position and your character in order to see if any building would intercept (and if it does make that part invisible). You can do that but if so it’d require you to make it so that all buildings are in a folder and if you do detect you’re in a building, make all the parts in said folder opaque or transparent. The other method is similar to your touched event but more reliable. :GetTouchingParts() returns an array (a list of data, in this case baseparts in the workspace) that are touching the part you called :GetTouchingParts() on. So if you call :GetTouchingParts() on your characters rootpart, you can detect if you’re in a building via those bricks you have in the example.

If you wish I can explain in detail how to Raycast and how to utilize GetTouchingParts() both are very viable methods.

My head hurts. Everybody keeps telling me different things and I can’t keep up, even if you go in to detail about raycasts I don’t think i’ll be able to make a working script.

Don’t say that man, programming is all about experimenting and figuring solutions out, its a tedious process but we’ve all been there. Raycasting is WAY more simple than it sounds, trust me on that :smile:

Raycasting is basically as I said before drawing a line from a starting point in a direction that you’ve determined. The goal of that is to see if it’ll intercept anything. This is used commonly for gun scripts, lasers, etc.

local player = game.Players.LocalPlayer

local camPos = workspace.CurrentCamera.CFrame.Position
local rootPos = player.Character.HumanoidRootPart.Position

local result = workspace:Raycast(camPos,(rootPos-camPos).Unit*250)

Breaking this down line by line.

I define the player and get the position of your camera and your characters root position, the humanoidrootpart. Then I call the :Raycast() of the workspace. I determine the start point which is your cameras position. Then I draw a line in the direction of your characters root part. By subtracting your root position by the camera position and using the Unit function, I’ve creating a directional value from your cam to your player. I multiplied it by 250 so it goes out by 250 studs. High values are taxing on the game and will cause lag. Now I’ve defined that whole function as a variable “result” thats because the function returns a few things back for you. The ones you’d be interested in is Instance.

local player = game.Players.LocalPlayer

local camPos = workspace.CurrentCamera.CFrame.Position
local rootPos = player.Character.HumanoidRootPart.Position

local result = workspace:Raycast(camPos,(rootPos-camPos).Unit*250)

if result then -- check if we hit something
    local character = result.Instance.Parent -- defining the character, you get the part that was hit but the character is a model, so you'd go part.Parent.
    local humanoid = character:FindFirstChild'Humanoid' -- this is a way to tell if it is a character, if you can find the humanoid
    if humanoid then -- checks if the humanoid is present, if so, you've found your character
        print'nothing blocking character'
    else -- else if theres no humanoid, it hit a building or something.
        print'object blocking character'
    end
end

I can elaborate further if need be, it can be a little daunting at the start but its relatively simple.