RaycastParams is not updating its filter and causing a script timeout

Hi there Recently I have been wanting to make a game like doors and I am far into development but I am in a holdup with Eyes, He requires ray-cast to see whether or not the player has view of him, But the way my map is setup, has parts that are invisible. And the Ray-cast are hitting those parts. So to counter this I made this function

local Raycast 
Raycast = function(EyesPos)
	local RayResult = RaycastModule.RayBetween2Points(Camera.CFrame.Position, EyesPos, RaycastParms)
	local Item = 0
	if RayResult then
		if RayResult.Instance.Transparency == 1 then
			RaycastParms.FilterDescendantsInstances["Item"..Item] = RayResult.Instance -- or table.insert(RaycastParams.FilterDescendantsInstances, RayResult.Instance)
			Item += 1
			return Raycast(EyesPos)
		else
			return true
		end
	else
		return false
	end
end

witch utilizes this function

module.RayBetween2Points = function(Position1,Position2,RaycastParms)
	local Origin = Position1
	local Direction = -(Position1 - Position2).Unit
	local Distance = (Position1 - Position2).Magnitude
	return workspace:Raycast(Origin,Direction*Distance,RaycastParms)
end

as seen by my ray-cast there is RaycastParams, There is RaycastParams. BUT that’s where the issue starts.

When I do anything to add RayResult.Instance including
table.Insert() This is what I would do normaly
Params.FilterDescendantsInstances[] This is what I do in the example.

I tried both of these and nether of them actually prevented the Raycast from hitting the invisible parts. This resulted in the function timing out the script, I have no idea why, Dose anybody know how to fix this?

Another note, This entire script is running on the CLIENT and is required to be run on the client due to the script using the cameras position, And getting the Screens Size from the player mouse.

Do you ever define Camera and RaycastParms?

Yes I do, I guess it might be useful if I provide the entire script

don’t worry about any of the Debug module things because that has no interaction with the raycasting

local RepSotrage = game:GetService("ReplicatedStorage")
local Player = game:GetService("Players").LocalPlayer


local DebugUI = require(Player.PlayerGui:WaitForChild("DebugUI"):WaitForChild("DebugUIMain"))
local RaycastModule = require(RepSotrage.Modules.RaycastModule)

local Mouse = Player:GetMouse()

repeat wait() until Player.Character

local Humanoid = Player.Character:WaitForChild("Humanoid")

local Camera = workspace.CurrentCamera

local UI1 = DebugUI.MakeNewDbugUI()
local UI2 = DebugUI.MakeNewDbugUI()
local UI3 = DebugUI.MakeNewDbugUI()
local UI4 = DebugUI.MakeNewDbugUI()
local UI5 = DebugUI.MakeNewDbugUI()
UI1.Text = "<b> EYES </b>"

UI2.Text = "N/A"
UI3.Text = "N/A"
UI4.Text = "N/A"
UI5.Text = "N/A"

local RaycastParms = RaycastParams.new()
RaycastParms.FilterDescendantsInstances =  {Player.Character}
RaycastParms.FilterType = Enum.RaycastFilterType.Exclude

local CurrentEyesModels = {}
RepSotrage.Remote_Events.EyesSpawned.OnClientEvent:Connect(function(EyesModel)
	table.insert(CurrentEyesModels,EyesModel)
	table.insert(RaycastParms.FilterDescendantsInstances,EyesModel)
	local TC
	TC = EyesModel.Destroying:Connect(function()
		table.remove(CurrentEyesModels,table.find(CurrentEyesModels,EyesModel))
		table.remove(RaycastParms.FilterDescendantsInstances,table.find(RaycastParms.FilterDescendantsInstances,EyesModel))
	end)
end)

local Raycast 
Raycast = function(EyesPos)
	local RayResult = RaycastModule.RayBetween2Points(Camera.CFrame.Position, EyesPos, RaycastParms)
	local Item = 0
	if RayResult then
		if RayResult.Instance.Transparency == 1 then
			RaycastParms.FilterDescendantsInstances["Item"..Item] = RayResult.Instance
			Item += 1
			return Raycast(EyesPos)
		else
			return true
		end
	else
		return false
	end
end


while wait(.2) do
	for i,q in pairs(CurrentEyesModels) do
		local EyesPos = q.Position
		local Pos = Camera:WorldToViewportPoint(EyesPos)
		local OnScreen = 0
		
		UI2.Text = Pos.X .. " : " .. Mouse.ViewSizeX  
		if Pos.X > 0 and Pos.X < Mouse.ViewSizeX then
			OnScreen += 1
			UI2.TextColor3 = Color3.fromRGB(55, 255, 0)
		else
			UI2.TextColor3 = Color3.fromRGB(255, 0, 4)
		end
		
		UI3.Text = Pos.Y .. " : " .. Mouse.ViewSizeY
		if Pos.Y > 0 and Pos.Y < Mouse.ViewSizeY then
			OnScreen += 1
			UI3.TextColor3 = Color3.fromRGB(55, 255, 0)
		else
			UI3.TextColor3 = Color3.fromRGB(255, 0, 4)
		end
		
		UI4.Text = tostring(Pos.Z > -1) 
		if Pos.Z > -1 then
			OnScreen += 1
		end
		
		UI5.Text = "false"
		if Raycast(EyesPos) == false then UI5.Text = "true" OnScreen -= 100 end
		
		if OnScreen >= 3 then
			Humanoid:TakeDamage(10)
		end
	end
end

To add things to the ray filter table, you have to use the built in function on RaycastParams, so you would do rayParams:AddToFilter({}) and put whatever you want to add in that table. Here is your code edited to use AddToFilter()

local Raycast 
Raycast = function(EyesPos)
	local RayResult = RaycastModule.RayBetween2Points(Camera.CFrame.Position, EyesPos, RaycastParms)
	local Item = 0
	if RayResult then
		if RayResult.Instance.Transparency == 1 then
			RaycastParms:AddToFilter({RayResult.Instance})
			Item += 1
			return Raycast(EyesPos)
		else
			return true
		end
	else
		return false
	end
end
1 Like

That solved it, One more question, Is there a function do Remove things from the filter

1 Like

Looks like the wiki doesn’t show anything for that. You’ll have to make your own way of ignoring certain things I guess, like doing an if statement on the RayResult.Instance

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