Raycast Wont Work

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…

Flip the part upside down then

When I test your code with the edit I made it works perfectly fine. It would be helpful to know what you are using this code for and if what I mentioned below doesn’t fix your issue have a repro file which reproduces the issues you are facing. This is how I am testing your code:

Raycast Fix.rbxl (21.5 KB)

As you are using part.CFrame.UpVector the ray wont go directly up from the part but instead go the direction of the UpVector. It could be possible that the part is upside down. Try flipping the part over or if the parts orientation doesn’t matter change the 10th line to this so the ray always goes directly up:

local raycastResult = game.Workspace:Raycast(part.Position, Vector3.new(0, 1, 0) * 50, raycastParams)
1 Like

Nope, I flipped the part in all directions and nothing happened. No errors, no prints…

It’s moments like these that feel so frusstrating about scripting because you could’ve done everything right and then u suspect that it’s the language code that’s bugged and not urs.

1 Like

Yeah, I couldn’t figure it out for 4 days, which is delaying my learning process. I tried to avoid as much as I can.

It worked! Thanks soo much! :smiley: