raycastResult working, raycastResult.Instance is not

For a test world, I have tried to use raycasting just to find a block ahead, nothing else. It’s a test for a different thing which relies on raycastResult.Instance working.

print("3")

print(raycastResult)

print(raycastResult.Instance)

print("4")

is the code and


this has been the result. The raycasting operation, as you can see from the screenshot, should’ve worked properly (unless I’m missing something quite obvious) but when I try and print raycastResult.Instance it prints nothing.
I have tried setting it to a variable then printing; I get the same output.
Any help is appreciated :slight_smile:

Yeah this is quite confusing it has to be said. The ray does seem to be hitting something as the material is plastic and the usual ray cast result things are displayed.
Could it be a spelling mistake in the actual script?

-- var
local origin = script.Parent.Position
local finalval = script.Parent.CFrame * CFrame.new(0,0,50)
local final = finalval.Position
local children = workspace.detectors:GetChildren()
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.IgnoreWater = true
raycastParams.FilterDescendantsInstances = children

local raycastResult = workspace:Raycast(origin, final, raycastParams)

print("3")

print(raycastResult)

print(raycastResult.Instance)
print("4")

Here’s the entire unchanged code
script.parent is a BasePart from which the ray is being fired
finalval is just for working
final is the target of the ray
detectors contains the base part for which is the target
Rest is self explanatory.

1 Like

Thanks for the code, what happens when you try removing the instance filtering?

I’m getting the exact same thing in the output which is annoying

I found a slight workaround to the problem shown below

-- the below code will print nothing
print(raycastResult.Instance)
-- you can instead do
print(raycastResult.Instance:GetFullName()

roblox is funky

3 Likes

That’s interesting and maybe indicative of a bug on roblox side of things. Does print(raycastResult.ClassName) produce the right thing?

After continually forgetting to do it, I’ve finally tested it and have got some strange results.


It seems to ignore the second print(raycastResult) - far from what I expected. There are not any errors related to this bit of code - only some remnants from some other script.

Old thread but i’m having the same issue, no idea how to fix it

Hi, I managed to get it working - can you provide a short snippet of your code so that I can see what went wrong? In my case it was not understanding what an instance was - raycastResult.Instance when it has hit a part will return Instance | Roblox Creator Documentation and you can get the properties of that through raycastResult.Instance.Name for example.

Hello! i found a work around. Instead i used created a part at the Mouse.Position. (Im making a gun.)
Then i used a Touched() function on the part.