Raycast not ignoring IgnoreTable

hey is it normal that my raycast isnt ignoring my ignoreTable, it makes the part move to my cam:

https://gyazo.com/5ea0e7583a1d8c6ad93c1ed518203833

local player = game:GetService("Players").LocalPlayer
local remoteEvent = script:WaitForChild("RemoteEvent")
local mouse = player:GetMouse()
local runService = game:GetService("RunService")

runService.RenderStepped:Connect(function()
	remoteEvent:FireServer(mouse.Hit)
end)

local character = player.Character or player.CharacterAdded:Wait()
local part = workspace:WaitForChild("followPart",5)

remoteEvent.OnClientEvent:Connect(function(player)
	runService.RenderStepped:Connect(function()
		local raycastParams = RaycastParams.new()
		local ignoreTable = {character}
		raycastParams.IgnoreWater = true
		raycastParams.FilterDescendantsInstances = {part, ignoreTable} --line where ignores part
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		local unitRay = mouse.UnitRay
		local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, raycastParams)

		if raycastResult then
			part.Position = raycastResult.Position
			part.Position += (part.Size / 2) * raycastResult.Normal
		end
	end)	
end)
1 Like

FilterDescendantsInstances only accept Instances as values in an array, you can unpack the ignoreTable to get around this:

raycastParams.FilterDescendantsInstances = {part, table.unpack(ignoreTable)}

Though I have to ask, why create another separate table for ignoring character parts in the same scope when ignoreTable is being used only once?

1 Like

i dont remember why i used separate table to ignore character. but the script line u sent isnt working

{part, character}

Just do it like this.

I’m not sure how it isn’t working for you. When I disable remote event related code and only run the raycast related code it works as intended.

Unless you’re talking about how raycast just goes through the character and place the followPart behind the character from the perspective the the camera, but that’s the expected behaviour when you put the character in ignore list, raycast just goes through the character. If you want to do something else to the followPart when cursor is on character then you will have to modify your code to check if part that raycast hit is a descendant of the character model instead of blacklisting it.

if raycastResult.Instance then

workspace:Raycast() can return nil.

This isn’t related to your issue, just a change you should make.

"workspace:Raycast() can return nil." part is correct, but the OP already accounted for that already with “if raycastResult then” line already.

On top of that your code is rather pointless as if raycastResult isn’t nil, raycastResult.Instance cannot be nil either as otherwise that would imply that raycast actually didn’t hit anything, which would make the entire point of returning raycastResult pointless.

I did mention the change wasn’t necessary, personally I find it more intuitive. Wouldn’t calling table.unpack() on an array which contains a single value also be pointless? Moreover the overhead from calling a table library function is going to be much more significant than indexing a key/field in a dictionary.

Yeah that’s normal since FilterDescendantsInstances expects a table of instances. Why can you not just compose the table with the part and the character? It doesn’t seem like you’re arbitrarily adding any new instances to your ignore lists, neither the created one or the one given to RaycastParams.

If you need to form an aggregate table because you expect to be dynamically adding instances to your ignore list, you can look at table.move. This will allow you to move elements from one table to another and is the recommended way of forming aggregate filter lists. I mean there’s also unpack but… you supposedly said it didn’t work (and didn’t really give any context on how it wasn’t working).

Example:

local foo = {"hi"}
local bar = {"bye"}

local recomposed = table.move(bar, 1, #bar, #foo + 1, foo)

print(recomposed) --> {"hi", "bye"}

Personally I’m more scared of the way you’re using events here. You’re using RenderStepped so that’s already somewhat of a concern but even more concerning is that you’re connecting to RenderStepped every time the RemoteEvent is fired without disconnecting other connections or anything.

You should only have one RunService connection going with the RemoteEvent being able to push an object to be updated during that connection rather than making the connection when the event fires. That or at the very least implement some connection management so you don’t suffer from a memory leak from steps you don’t need anymore (and perhaps it may even be related to your issue).

2 Likes