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
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.
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
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.
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
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?
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.
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
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:
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)
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.