Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is FindPartOnRay not working?

This is just a script to make curtains work. Most of it isn't really needed to know for the problem. The problem is at line 36.


----------Variables------------------

top = script.Parent


opened = top:WaitForChild("Opened")
closed = top:WaitForChild("Closed")
event = top:WaitForChild("String")

action = false
debounce = false
listenTo = opened.Changed:connect(function() end)
listenTo:disconnect()

switch = top:WaitForChild("Switch")
curtainScript = switch:WaitForChild("Action")

currentColor = switch.BrickColor
clickedColor = BrickColor.new("Really red")

----------Variables for curtain Setup------------------------

parts = top:WaitForChild("CurtainParts")

railing = top:WaitForChild("Railing")
rWidth = railing.Size.X

test1 = railing.Position - Vector3.new(0,2,0)
print(test1)

test2 = railing.Position - Vector3.new(0,4,0)
print(test2)

ray = Ray.new(railing.Position, Vector3.new(0,-2,0)).unit

place = game.Workspace:FindPartOnRay(ray, nil)

print(place)

The problem is that it always sends back nil. The ray is supposed to look strait down from railing, but it always returns nil. I surrounded railing with bricks to see if it was going in the wrong direction, but it was still nil. Even substituting the railing position for test1 (which is just the railing Position lowered by 2) doesn't change the result. What am I doing wrong?

1 answer

ROBLOX's definition of "ray" is really more like a segment.

FindPartOnRay will only look as far as the length of the ray.

That means you probably don't want to use unit with it, and the second parameter should be multiplied by something big; at the same time, ROBLOX limits ray length to 1000 for raycasting.

Usually the construction looks like this as a result of all of the above:

local ray = Ray.new(start, direction.unit * 1000)

This discussion is preserved as it was. New answers, comments, and votes are closed.