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