I have a script that controls multiple NPCs and it works for the most part, but I have a problem with the Raycasting. The function where the Raycast is done runs when a tag is added in CollectionService which tells a spawned NPC to move to the Raycast instance. The problem is that the Raycast doesn’t work for some of the NPCs that spawn, but it works for most of them. I also have another problem in the same function. The NPCs goal is to destroy a wall and if a section of the wall is destroyed they are all supposed to move to a position behind the wall. The problem with this is that some of them react when the wall is destroyed, but most of them do not move at all.
Here is the function I have a problem with:
RayCast function
function attackPosition(obj)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {workspace.CloneFolder, workspace.Baseplate}
params.IgnoreWater = true
local rayResult = workspace:Raycast(obj.HumanoidRootPart.Position, (obj.HumanoidRootPart.CFrame.LookVector).Unit*600, params)
if rayResult.Instance:IsDescendantOf(Models.Barriers) then
moveTo(obj.Humanoid, rayResult.Position)
obj.Humanoid.MoveToFinished:Wait()
wallDestruction(obj,rayResult.Instance)
repeat wait() until Breach.Value == true
obj.Head.Material = "Neon"
obj.Head.BrickColor = BrickColor.Red()
rayResult.Instance.Velocity = rayResult.Instance.CFrame.LookVector * -50
local getPath = findPath(obj.HumanoidRootPart, workspace.BaseCore)
local waypoints = getPath:GetWaypoints()
for i, v in pairs(waypoints) do
obj.Humanoid:MoveTo(v.Position)
obj.Humanoid.MoveToFinished:Wait()
end
end
end