I need help, FindPartsOnRay is wrong

Here is download : Baseplate.rbxl (18.4 KB)

local main = script.Parent.main

local rock = 10
local range = 7

local function SpawnRock()
	for i = 1, rock do
		local Angle = (math.pi * 2) / rock * i
		local X_Pos = math.cos(Angle) * range
		local Z_Pos = math.sin(Angle) * range
		
		local oldpos = main.Position + Vector3.new(X_Pos, 0, Z_Pos)
		local ray = Ray.new(oldpos, oldpos - Vector3.new(0, 50, 0))
		local otherPart, position = workspace:FindPartOnRay(ray, script.Parent, false, true)
		
		local lineBb = Instance.new("Part", workspace)
		lineBb.Position = oldpos - Vector3.new(0, 5, 0)
		lineBb.Size = Vector3.new(0.15, 0.15, 0.15)
		lineBb.Anchored = true
		lineBb.BrickColor = BrickColor.new("Really red")
		
		local lineA = Instance.new("Part", workspace)
		lineA.Position = oldpos
		lineA.Size = Vector3.new(0.15, 0.15, 0.15)
		lineA.Anchored = true
		
		local a = (oldpos - position).Magnitude
		local b = Instance.new("Part", workspace)
		b.CanCollide = false
		b.Anchored = true
		b.Size = Vector3.new(0.1, 0.1, a)
		b.CFrame = CFrame.new(oldpos, position) * CFrame.new(0, 0, -a / 2)
		
		local lineB = Instance.new("Part", workspace)
		lineB.Position = position
		lineB.Size = Vector3.new(0.15, 0.15, 0.15)
		lineB.Anchored = true
	end
end

SpawnRock()

I want the line (is local b) reach the “lineBb” but idk how to fix it, can someone help me.
Idk my script is wrong or FindPartsOnRay is wrong…

What’s the problem here? How is it wrong? What have you attempted so far in trying to fix this? It doesn’t seem like you scrutinised your code enough or applied basic debugging. There is a severe lack of context as well.

Please remember that the Scripting Support category is not for you to dump entire place files and code blocks then ask for fixes. You need to put in your own efforts as well to debug this or, if you have indeed tried, include information to help us help you.

1 Like

It looks to me like the direction for the Ray is not correct.
I seem to recall seeing a lot of examples which make use of the LookVector of the sending part + some distance limit.

As RamJoT indicates, it looks like the second argument you provide to Ray.new() might be wrong:

Ray.new(oldpos, oldpos - Vector3.new(0, 50, 0))

According to https://developer.roblox.com/en-us/api-reference/datatype/Ray the second argument should be a ‘direction’, though it looks like you supply it with a ‘position’ in your code.

I have not tried it myself, but try changing it to this:

-- Start at position 'oldpos', with (normalized) direction "down" (Y-axis)
Ray.new(oldpos, Vector3.new(0, -1, 0))
1 Like

Thanks you, i was think it position…