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
The FilterType is Include, not Exclude: that means that only the baseparts included in FilterDescendantsInstances will be considered.
(taken from this article)
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
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.
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?
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