workspace:Raycast() not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to raycast normally.
  2. What is the issue? Include screenshots / videos if possible!
    It returns nil
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve copied youtubers’ code and it still doesn’t do anything at all

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local part = script.Parent

local origin = part.Position
local direction = part.CFrame.LookVector * 10

local raycastResult = workspace:Raycast(origin, direction) -- returns nil

if raycastResult then
	print(raycastResult.Instance)
else
	print("WTH DUDE")
end

After many failures I decided to look up tutorials but it didn’t help me, so thank you in advance if you can help me

Try increasing the multiplier distance in direction

no it didn’t do anything. Even if the raycast is short it would return a ray

local part = workspace.part

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {part}
raycastParams.IgnoreWater = true

local origin = part.Position

local direction = part.CFrame.LookVector * 10

local raycastResult = workspace:Raycast(origin, (direction - origin).Unit * 1000, raycastParams)

if raycastResult then
  print("hit")
else
  print("not hit")
end

Wrote this for you, try something like this.

–EDIT2–

Fixed params because I pasted them from something else, and made the script easier to read.

1 Like

I tried something like this, if I remove everything inside the blacklist, it works, Otherwise it will still be nil

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {workspace.Baseplate}
raycastParams.IgnoreWater = true

local part = script.Parent

local origin = part.Position

local direction = part.CFrame.LookVector * 10

local raycastResult = workspace:Raycast(origin, (direction - origin).Unit * 1000, raycastParams)

if raycastResult then

	print("hit")

else

	print("not hit")

end

like this for example, I added baseplate but it still prints not hit. But when I remove it, it prints “hit”

local part = script.Parent

game:GetService("RunService"):Connect(function()
local origin = part.Position
local direction = part.CFrame.LookVector * 10

local raycastResult = workspace:Raycast(origin, direction) -- returns nil
if raycastResult then
	print(raycastResult.Instance)
else
	print("WTH DUDE")
end
end)

Runservice can only be fired in local, and it must be like RS.RenderStepped, however if I remove the event, the raycast still returns nil

That’s weird. Maybe you’ve made a mistake or something, those things go easily unnoticed.

If you add something to the blacklist,

  • Enum.RaycastFilterType.Blacklist — Every BasePart in the game will be considered except those that are descendants of objects in the filter list.

and the said part is hit, it will come back as if you didn’t hit anything. That’s the easiest way to explain it.

game:GetService(“RunService”).Heartbeat

and print result.Instance.Name

Of course it is weird, I looked through my scripts many times.
This pic might be a clue if I made any mistake

Is the caster looking in the right direction?

Here is the right picture, I forgot things to contain

Oh yeah, the part should be facing forward

1 Like

image
I inserted a decal and sure enough its in the wide side of the part

1 Like

alright the problem is solved :skull:

1 Like

I had another problem sorry

local origin = part.Position

local direction = part.CFrame.LookVector * 10

local raycastResult = workspace:Raycast(origin, (origin - workspace.CollidePart.Position).Unit * 1000, raycastParams)

if raycastResult then

	print("hit")
	print(raycastResult.Instance)

else --......

what could be wrong here?

What’s the issue? We can’t help you if we don’t know what’s wrong.

Again I cant raycast, I added unit thing you can see and it stopped working

No, RunService can be used in both server and client. The only event that fires on the client is RenderStepped, while Stepped and Heartbeat can be used in both.

Try the other way

workspace.CollidePart.Position - origin
1 Like

It worked perfectly, thank you all for everything