How to Avoid Walls In Raycasting

How do I make it not detect players through walls???

local uis = game:GetService("UserInputService")

local localplayer = game.Players.LocalPlayer
local char = localplayer.Character or localplayer.CharacterAdded:Wait()

local enabler = script.Parent
local enabled = false

local camera = workspace.CurrentCamera
local mouse = localplayer:GetMouse()

function enabledisable()
	if enabled == false then
		enabled = true
		enabler.TextColor3 = Color3.new(0, 255, 0)
	else
		enabled = false
		enabler.TextColor3 = Color3.new(255, 0, 0)
	end
end

uis.InputBegan:Connect(function(input, processed)
	if not processed then
		if input.KeyCode == Enum.KeyCode.E then
			enabledisable()
		end
	end
end)

enabler.Activated:Connect(function()
	enabledisable()
end)

game["Run Service"].Stepped:Connect(function()
	if enabled == true then
		for _, player in pairs(game.Players:GetPlayers()) do
			if player ~= localplayer and player.Character and player.Character:FindFirstChild("Humanoid") and player.Character.Humanoid.Health > 0 then
				local screenPos, onScreen = camera:WorldToScreenPoint(player.Character.HumanoidRootPart.Position)

				if onScreen then
					local mousepos = uis:GetMouseLocation()

					local screenPosVector = Vector2.new(screenPos.X, screenPos.Y)

					local distance = (screenPosVector - mousepos).Magnitude

					if distance < 125 then
						
						
						local direction = (player.Character.HumanoidRootPart.Position - char.HumanoidRootPart.Position).Unit * 1000
						
						local raycastParams = RaycastParams.new()
						raycastParams.FilterDescendantsInstances = {char}

						local result = workspace:Raycast(char.HumanoidRootPart.Position,direction, raycastParams)

						if result  then

						print("near")
						camera.CFrame = CFrame.new(camera.CFrame.Position, player.Character.Head.Position)
						end
						else
						print("far")
					end
				end
			end
		end
	end
end)
1 Like

Hi goofy! I’m not sure what your issue actually is. You want to detect if you’ve hit a player or a wall?

So, the only thing you’re filtering is the person who’s raycasting, meaning if you want to detect if you’ve hit a player you can just do this:

if game.Players:GetPlayerFromCharacter(result.Instance.Parent) then
    -- code here
end

Alternatively, if you want to count detecting an NPC, then you should do this:

if result.Instance.Parent:FindFirstChild("Humanoid") then
    -- code here
end

Also indent your code properly, I know when you send it to me it’s different, but on the DevForum that actually matters.

1 Like

If i hit a wall I dont want it to detect the player, therefore not chagning the cameras cframe

Then I gave the right solution, just put either of those snippets inside this:

if result then
end

so it should be this:

if result  then
	if game.Players:GetPlayerFromCharacter(result.Instance.Parent) then -- or "if result.Instance.Parent:FindFirstChild("Humanoid") then" for NPC detection
		print("near")
		camera.CFrame = CFrame.new(camera.CFrame.Position, player.Character.Head.Position)
	end
end
1 Like

worked great thanks alot! I thought about doing something like this but I guess im just pretty silly.

1 Like

youre very goofy, indent your code properly PLEASE IM BEGGING

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.