Goal:
My initial goal was to make a simple pathfinding system for a basic, moving AI. I know there’s already a built in pathfinding system, but I’ve wanted to make my own for a while now. I wanted it to send a ray out in a random direction (other than up and down of course) and to have the dummy move in that direction. If the ray hit something, it would move to that position -3 or so studs so as not to walk into the wall, if not, it’d just go to the ray’s end.
Problem:
Issue is, well, I’m not sure really. From my testing and everything I’ve tried, whenever the ray hits something, I get completely unexpected results and the dummy never acts as I expect. Every time I hit run, for example, it always goes in almost the same exact direction. I’ve tested probably like at least 50 times by now, and every time it’s gone left at the start. I wondered if this was due to lack of randomness in math.random, so I decided to use the Random.new() object provided by Roblox instead and even printed the degrees and that doesn’t seem to be the issue.
Videos + Images:
I’m really not sure how to explain what I mean, so here’s a gif of it in game: link
And yes, if you were wondering, the raycast is set to ignore the parts being made, so that’s not the issue; I’ve also tested it with prints to make sure.
Also, as you can see here, the dummy started in the middle, the part indicates that the ray was sent in a random direction to the right-ish, but then the dummy ends up going into the same wall it always does and never gets out, despite the parts clearly indicating the rays are not touching that area.

And here’s another video, this time without the parts and with the dummy in a sort of maze thing.
What I’ve done:
As I’ve mentioned a little bit already, I tried changing up the method of generating random directions, and that doesn’t seem to be the issue. I’ve also tried various things, adjusting the end goal of the ray, making the ray start from behind the dummy in case the dummy walks into a wall and, no matter the direction of it, the ray manages to hit the part, and nothing I’ve tried seems to have done much. I got it to stop 3 studs before reaching the part, as I wanted, but then it just repeatedly walks nowhere.
Script:
I’m not sure if it’s a problem with raycasting, or just something I’m missing and/or doing wrong, but either way, here’s my script, a server script right inside the character model:
-- Character Stuff
local char = script.Parent
local hum = char.Humanoid
local root = char.HumanoidRootPart
-- Moving Randomly
movingActive = true
coroutine.resume(coroutine.create(function() -- I'm using a coroutine for later, when I add more than just moving, so the script doesn't get stuck on the loop.
local last = nil
local model = Instance.new("Model",workspace)
while true do wait(1)
if movingActive then
if last then
last:Destroy()
end
-- Getting Random Direction
local rand = Random.new(math.random(0,999999999))
local angles = CFrame.Angles(0,math.rad(rand:NextInteger(-360,360)),0)
local normal = angles.LookVector
-- Raycasting
local distance = rand:NextInteger(50,200)/10
local params = RaycastParams.new()
params.FilterDescendantsInstances = {char,model}
params.FilterType = Enum.RaycastFilterType.Blacklist
local result = workspace:Raycast(root.Position,
root.Position + normal*(distance),params)
-- Creating Part
local part = Instance.new("Part",model)
part.Name = "Direction"
--last = part
part.Material = Enum.Material.Neon
part.Anchored = true
part.CanCollide = false
-- Showing Hit Position
local part2 = part:Clone()
part2.Color = Color3.fromRGB(255,0,0)
part2.Size = Vector3.new(0.5,30,0.5)
part2.Parent = model
local pos = nil
local pos2 = nil
if result then
pos = result.Position + (result.Position - root.Position).Unit*-3
pos2 = root.Position + normal*distance
part2.Position = result.Position
else
pos = root.Position + normal*distance
pos2 = root.Position + normal*distance
part2.Position = root.Position + normal*distance
end
part.Size = Vector3.new(0.5,0.5,(pos2 - root.Position).Magnitude)
part.CFrame = CFrame.lookAt(root.Position + (pos2 - root.Position).Unit*(
part.Size.Z/2),pos2)
-- Moving
hum:MoveTo(pos)
hum.MoveToFinished:Wait()
end
end
end))