Raycast problems, Npc pathfinding

Hello guys,
i want to make a game with npc’s walking on a path i want to use raycasting to find the potential 4 paths next to the current one. i have a script here:

local folder = script.Parent

local currentPart = script.Parent.start

local pos = {Vector3.new(100,0,0), Vector3.new(-100,0,0), Vector3.new(0,0,100), 
Vector3.new(0,0,-100)}

local function FireRays() -- fires 4 rays on all the sides of the part and chooses with path to walk 
towards.
    local rays = {}

for i = 1, 4 do
	local ray = workspace:Raycast(currentPart.Position, currentPart.Position + pos[i])
	if ray then
		if ray.Instance.Parent == folder then
			table.insert(rays, #rays, ray.Instance)
		end
	end
end

	if #rays > 0 then
		local path = math.random(1,#rays)
		currentPart.Color = Color3.fromRGB(255, 255, 255)
		currentPart = rays[path]
		print(currentPart)
	end
end

while wait(1) do
	FireRays()
	currentPart.Color = Color3.fromRGB(255, 192, 43)
end

but everytime i run the code the same part is detected even if the path value is difrent as 1

It might be because the ray is hitting currentPart. You could filter it out with RaycastParams and try that!

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {currentPart}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local ray = workspace:Raycast(currentPart.Position, currentPart.Position + pos[i], raycastParams)

Also I’m not entirely sure you have the second parameter correct there. Raycast works differently that CFrame does, the second parameter is just a direction vector. Meaning you probably don’t need to have currentPart.Position + pos[i] and instead just pos[i] but you might be doing something specific so I could be wrong on that one.

i’ve tryed that too with blacklisting the part but it also didn’t work

edit: the ray parameter goes from origin to the pos[i] so that should’t be the problem the first ray works but the array is full of only 1 raycast result 4 times or so