Raycast Wont Work

Hello Developers! :wave:

My raycast wont work:

local part = game.Workspace.Part
local raycastParams = RaycastParams.new()

raycastParams.FilterDescendantsInstances = {game.Workspace.Baseplate}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist


while true do
	wait(1)
	local raycastResult = game.Workspace:Raycast(part.Position, part.CFrame.UpVector * 50, raycastParams)

	if raycastResult then
		print("Ray has been touched by " .. raycastResult.Instance)
    end
end

What exactly do you mean by not working? Is it erroring or is it not printing something when it’s supposed to?

1 Like

It’s not erroring, and it’s not printing anything in the output.

RaycastResult is only returned if the ray had successfully intersected something. What object is the ray supposed to be targeting?

Also check:

  • The direction of the ray
  • The length of the ray being able to reach a target
  • The script itself, if it’s even running properly(the while loop)

Try using (part.CFrame * part.CFrame.UpVector) * 50

1 Like

Doesn’t work somehow. I don’t know why…

The direction of the ray is up, above the part. 50 studs up. The while loop is working, I used print statements to determine which part of the loop works. And the problem of the script is this:

if raycastResult then
	print("Ray has been touched by " .. raycastResult.Instance)
end 

It wouldn’t print anything in this if statement and it is in the while loop. Help me please :grimacing:

Try printing the raycastResult? Also add a else statement with another print if there isn’t a RaycastResult

After testing your code in a blank baseplate I noticed the print statement on line 13 errors. This could be the reason why your code doesn’t work.

image

Try changing line 13 to this:

print("Ray has been touched by " .. raycastResult.Instance.Name)

Full code:

local part = game.Workspace.Part
local raycastParams = RaycastParams.new()

raycastParams.FilterDescendantsInstances = {game.Workspace.Baseplate}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist


while true do
	wait(1)
	local raycastResult = game.Workspace:Raycast(part.Position, part.CFrame.UpVector * 50, raycastParams)

	if raycastResult then
		print("Ray has been touched by " .. raycastResult.Instance.Name)
	end
end

Ok, so I wrote this:

while true do
	wait(1)
	local raycastResult = game.Workspace:Raycast(part.Position, part.CFrame.UpVector * 50, raycastParams)
	
	print(raycastResult)

	if raycastResult then
		print("Ray has been touched by " .. raycastResult.Instance)
	else
		print("Nothing touched")
	end
end

It’s printing in the output nil and nothing touched. Even if a part intersected with the ray.

I didn’t get anything and I got no errors in the output.

My guess is that 1 of the parameters in the raycastResult is a invalid argument or something? Try just doing Vector3.new(0, 50, 0) instead of part.CFrame.UpVector * 50 for the second argument

1 Like

No, it just prints nothing is intersecting with ray. And I tried to change UpVector and Vector3 with lookvector. But still doesn’t work.

Ok, how about trying this instead?

  • Place another Part inside the workspace, move it 20 studs up & make the size large so that it’s detectable from where the original Part is supposed to be

  • Detect if that Part will intersect the Ray if possible?

It kinda worked. I wrote:

print("Ray has been touched by " .. raycastResult.Instance:GetFullName)

With the function getfullname. It printed the I touched the ray with my right arm. But it kinda works and kinda doesn’t.

Ok, it worked. I made a part and named it “DemoPart”. Then it printed, “Ray has been intersected by DemoPart”. But, when I remove the part in studio with the move tool… It still prints the same thing, even though I moved it away from the ray. I jumped on the part and it wont print my name.

Ok, here’s what my guess is then for the reason why the raycastResult printed as nil:

  • You defined FilterDescendantsInstances inside the Baseplate, and the FilterType was equal to a Blacklist which basically ignored the Baseplate

  • Since there wasn’t a proper raycastResult, it resulted as nil

You could just remove the raycastParams and see if that changes anything at all?

local part = game.Workspace.Part
local raycastParams = RaycastParams.new()

raycastParams.FilterDescendantsInstances = {game.Workspace.Baseplate}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist


while true do
	wait(1)
	local raycastResult = game.Workspace:Raycast(part.Position, (part.Position + part.CFrame.UpVector * 50), raycastParams)

	if raycastResult then
		print("Ray has been touched by " .. raycastResult.Instance)
    end
end

Doesn’t work. It still prints even though I used the move tool to remote the demopart in-game.

It doesn’t even print anything in the chat…