Ray hitting player, despite ignoring player Character?

Hey guys, I have been working on a gun, and all seems well until the ray looks for a part to hit. Despite being told to ignore player.Character, hitPart.Name still prints Y35X (my username) Here is the code that I am using.

local ray = Ray.new(tool.Hole.CFrame.p, (mouse.Hit.p - tool.Hole.CFrame.p).unit * configurations.Range.Value)
	local hitPart, position = workspace:FindPartOnRay(ray, player.Character, false, true)

the ray is being shot from a point on my pistol, nowhere near any body part, which makes me think that it is hitting a part of the gun, so I changed the script to this:

local ray = Ray.new(tool.Hole.CFrame.p, (mouse.Hit.p - tool.Hole.CFrame.p).unit * configurations.Range.Value)
	local ignore = {player.Character, tool:GetChildren()}
	local hitPart, position = workspace:FindPartOnRay(ray, ignore, false, true)

considering my luck though, instead of the ray actually working now, it throws an error:
“Unable to cast Value to Object”.

How can I fix this issue?

EDIT: I do not know if this matters, but all of this occurs within FirstPerson, as my game is locked to that.

You should use WorldRoot | Documentation - Roblox Creator Hub

local ray = Ray.new(tool.Hole.CFrame.p, (mouse.Hit.p - tool.Hole.CFrame.p).unit * configurations.Range.Value)
	local ignore = {player.Character, tool:GetChildren()}
	local hitPart, position = workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)

lol posted the exact same thing at the exact same time

If you want to ignore the player’s character, you should use :FindPartOnRayWithIgnoreList().

It still turns up the same error, and to be redundant and 100% sure that it isn’t simply a syntax issue, I mentioned each individual part of the character’s children, same thing with the tool. Thus the same error is given.

@FlashFlame_Roblox @Dev_Kittenz

As :FindPartOnRay can already take these arguments including ignoreDescendantsInstance, it would not make a difference for the problem to replace this function with its counter function :FindPartOnRayWithIgnoreList instead.

:FindPartOnRay with all its available arguments:

:FindPartOnRay(ray, ignoreDescendantsInstance, terrainCellsAreCubes, ignoreWater)

@Y35X Wants to ignore multiple objects, not only one. :FindPartsOnRay() accepts Instance as parameter and not a table.

I was referring to this:

Where they only wanted to ignore the player.Character.

Have you tried printing the name of the hitPart?

Ignoring player.Character still turns up as “Y35X” when I print the name of the hit part, so I assumed that also ridding tool might get rid of this issue.

Yes, it prints as “Y35X” when there is no error. The only time there is an error is when I make it explicitly clear to ignore all character children too with :GetChildren()

It should really not print your username as hitPart cannot be a model or a player instance. That’s why I asked.

Yeah, it should print “Left Leg” or “Head”. I also made sure that I didn’t accidentally say to print hitPart.Parent

Then there’s what you need to figure out first. Either provide your code for us to try and solve it, or debug by yourself why it prints your username instead of the name of a basepart.

I have to go now, but tomorrow I’ll try debugging it. If I come to no avail devforum will be my Plan B.

1 Like

Pretty sure this is an illegal argument, I never recall the ignore list of raycast functions supporting nested tables. This part needs to change. So long as you do not put any other elements after the GetChildren call, you can unpack the table and catch the returns in the table.

{player.Character, table.unpack(tool:GetChildren())}

Aha, after studying my code some, on the server script, I accidentally put hitPart as the first argument, which is reserved for the player. So it looks like this:

functions.Shoot.OnServerEvent:Connect(function(hitPart)

when in reality I should have put these for the arguments:

functions.Shoot.OnServerEvent:Connect(function(player, hitPart)

Note: I should add that the script printed the hitPart name within this function, so it wasn’t a client issue all along.