Why does my raycast activate through walls?

hello,
after making THREE posts and not getting help on any of them this is my last attempt at making a post so hopefully, someone can help me out here

I’m making a slenderman game on Roblox and I’m trying to get the static GUI to work when slender is being looked at. However the GUI can still activate through walls for some reason even though I scripted the raycast stuff.

here’s my code (the whole thing lol):

local camera = workspace.CurrentCamera
local mouse = game.Players.LocalPlayer:GetMouse()

local p = game.Players.LocalPlayer

local c = p.Character or p.CharacterAdded:Wait()


local VisCheck = function(Obj)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	raycastParams.IgnoreWater = true
	local raycastResult = workspace:Raycast(camera.CFrame.Position, CFrame.new(camera.CFrame.Position,Obj.Position).LookVector*5000, raycastParams)
	if raycastResult ~= nil then
		if raycastResult.Instance:IsDescendantOf(Obj.Parent) then
			if Obj.Parent.Humanoid.Health > 0 then
				return true
			end
		else
			return false
		end
	end
	return false
end

local StartUITrack = function(Obj)
	local worldPos = Obj.Position
	local pos, isOnScreen = camera:WorldToScreenPoint(worldPos)
	local fixedposx = pos.x
	local fixedposy = pos.y
	local IsInLib,num = false, nil

	if isOnScreen then
		if VisCheck(Obj)== true then
			return true
		end
	else 

	end

	return false
end







while wait() do
	
	local mag;
	
	local range = game.Lighting.FogEnd - 2;

	local cansee = true
	
	local DummyHead = workspace.Slenderman["Meshes/torso (3)"]
	
	coroutine.wrap(function()
		while wait() do
			mag = (c.Torso.Position - DummyHead.Position).magnitude;
		end
	end)()
	
	local WG = workspace.Slenderman
	
	coroutine.wrap(function()
		while wait() do
			local Position, CanSee = camera:WorldToScreenPoint(workspace.Slenderman["Meshes/thehead"].Position)
			if CanSee and mag <= range and cansee then 

				local StaticMain = script.Parent.StaticGUI
				local Static1 = StaticMain.Static1
				local StaticSound = script.StaticSound

				StaticSound.Playing = true
				StaticSound.Looped = true

				StaticSound.Volume = 1 /mag * 2

				StaticMain.Enabled = true
				Static1.Visible = true
				Static1.ImageTransparency = 0.50 * mag / 8.55
				wait(0.1)
				Static1.ImageTransparency = 0.75 * mag / 8.55
				wait(0.1)
				Static1.ImageTransparency = 0.25 * mag / 8.55
				wait(0.1)
				Static1.ImageTransparency = 0.9 * mag  / 8.55
				wait(0.1)

			else

				local StaticMain = script.Parent.StaticGUI
				local Static1 = StaticMain.Static1
				local StaticSound = script.StaticSound

				StaticMain.Enabled = false
				StaticSound.Playing = false

			end;
		end;
	end)()

	local IsSeen = StartUITrack(WG["Meshes/thehead"])
	if IsSeen == true then

	end

end

any help is greatly appreciated. thank you in advance

So are you seeing if the player can see slender? Wouldn’t you want it so it checks if slender can see the player? Also you might wanna use Camera.CFrame.LookVector instead but im not sure.

how can I use this in my already written code?

I was thinking on your raycast result line

alright, i’ll try that. I’ll update you on the situation

also you never my answered my question in my first reply

basically i’m trying to make it where when the player is looking at slender, a GUI triggers

try this

local raycastResult = workspace:Raycast(camera.CFrame.Position, camera.CFrame.LookVector*5000, raycastParams)

nope, still activates through walls.

how exactly are you testing it?

I think the reason its going through walls is because of your “Obj” variable. print Obj.Name and see what that returns.

1 Like