I’m trying to make an AI that attacks players that it sees using raycast, But the RaycastResult is nil for some reason. Here is my code.
code.lua (552 Bytes)
I’m trying to make an AI that attacks players that it sees using raycast, But the RaycastResult is nil for some reason. Here is my code.
code.lua (552 Bytes)
May you please change the code to copy and paste in the topic? For the best help, make sure your code can be seen by PHONES too.
if Humanoid and Humanoid.RootPart then
local magnitude = (Hostile.PrimaryPart.Position - Humanoid.RootPart.Position).Magnitude
if magnitude <= DetectionMagnitude then
local rp = RaycastParams.new()
rp.FilterDescendantsInstances = {Hostile, v.Character}
rp.FilterType = Enum.RaycastFilterType.Blacklist
rp.IgnoreWater = true
local sight = workspace:Raycast(Hostile.Raycastpoint.Position, Humanoid.RootPart.Position, rp)
if sight then
print(sight.Instance)
end
end
end
I’m new to the DevForum I don’t really know how to like separate code from normal text. (I found it nvm)
code
But I’m really confused on why the RaycastResult is nil even though I’m doing it correctly.
Does this print? Nil? Try putting random prints for debugging to see where your code stops.
It doesn’t print at all, Which means it is nil.
Try putting a print above this line and check if it prints.
It runs the Raycast code but the RaycastResult is nil.
Actually the Raycast is returning nil because of 2 reasons:
Not in range or
Target is blacklisted
Possibly blacklisted
By the way, I put print(sight) without an if statement, and it prints nil.
It’s supposed to detect walls between the target player and the hostile model.
It’s returning nil even tho it’s in range, And the target is blacklisted because the raycast is supposed to detect stuff thats obstructing the view of the hostile model.
Also, Even if the target is behind a wall, It’s still returning nil.
I suppose you want the Raycast to detect
But the Character is blacklisted, meaning it won’t detect it.
I am also a begineer on Raycasts, don’t really get how they work.
I want the raycast to detect objects between the target and the hostile model
Weird. I can’t think of any other reason why would the Raycast not return anything in it’s view.
Also I forgot to mention, When I remove the RaycastParams, It doesn’t return nil, But it returns the hostile models UpperTorso.
And if there is a wall behind the 2 of you? Would it return anything?