Raycast not detecting player

Hey, fellow devs!
I’m making an enemy that uses a magic wand to create a few light beams in a cross pattern that attack you. To detect if you should get damaged or not, I thought about using raycasts from 150 studs in the sky and go down perpendicularly by another 200. The problem is: the ray isn’t hitting anything!

Here’s my code:

-- This code executes when the attack begins
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
for _, player in pairs(game:GetService("Players"):GetChildren()) do
	table.insert(params.FilterDescendantsInstances,player.Character)
	print("inserted "..player.Name)
end





-- This code executes later, and once for each beam
local BeamPos = Target + Vector3.new(i,-.5,i*side)*2 -- Beampos is the position where the Beam hits
local result = workspace:Raycast(Vector3.new(BeamPos.X,150,BeamPos.Z),Vector3.new(0,-200,0),params)

if result then
	local Humanoid = result.Instance:FindFirstAncestorWhichIsA("Model"):FindFirstChild("Humanoid")
	if Humanoid and (table.find(hit,Humanoid) == nil) then
		Humanoid:TakeDamage(self:GetAttribute("Damage"))
		table.insert(hit,Humanoid)
	end
	
end

It has never done damage to anything. I have also tried using Shapecasts, but to no avail.

Help would really be appreciated!!!

EDIT: here’s a video demonstrating the problem.

If the ray hit, I have set up a print to say "Hit" in the console

Uhh, you’re ignoring the player’s character in the raycast params…

The FilterType is Include, not Exclude: that means that only the baseparts included in FilterDescendantsInstances will be considered.
(taken from this article)

Oh didnt see that sorry for the confusion

1 Like

double check your beam position, it might be because you’re raycasting the wrong spot

I have already tried visualizing the raycast, but it seems the positions and directions are correct.

try printing the instance the ray is hittin

There is no instance: there’s no result in the first place.
If I do something like

if result then
	print("Hit")
	local Humanoid = result.Instance:FindFirstAncestorWhichIsA("Model"):FindFirstChild("Humanoid")
	if Humanoid and (table.find(hit,Humanoid) == nil) then
		Humanoid:TakeDamage(self:GetAttribute("Damage"))
		table.insert(hit,Humanoid)
	end
	
end

the console doesn’t give any output.

you do know a ray lasts like 0.0001 seconds
maybe it doesnt hit anything

what exactly is self:GetAttribute("Damage")?
also pretty sure raycast will return the character part it hit, not the model

result.Instance:FindFirstAncestorWhichIsA("Model")

What would that have to do with anything?

self is the model of the enemy (script.Parent), and it has a numeric attribute named Damage which’s value is set to 30.
Also, yes, it returns the part: that’s why I find the first ancestor of the part of type model (in this case, the player character), and then get its humanoid.

Didnt you just say it returns no instance?

I was just answering the question, but yes, it doesn’t return anything.

Apparently, by switching to Enum.FilterType.Exclude it now works.
I hate that this is a valid solution, but it is what it is.

1 Like

That made me realize it could have been because of the way you were inserting the characters into params.FilterDescendantsInstances via table.insert. Did you check that if the array is correct?

1 Like

I have already checked that with breakpoints, it’s all fine. It was on of the first things that came to mind when I noticed it wasn’t working.

1 Like

using table.insert with a filter list actually does nothing (just found out after some testing), if it’s working correctly then the exclude should prevent the raycast from hitting your character

use params:AddToFilter(character)

1 Like

Thanks so much, this works too. I didn’t know it existed!

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