I am new to modulescripts.I used a modulescript for handling NPC blood and pathfinding, however it broke on the modulescripts module.MakePath(). The first input of the function which is npc, suddenly turned into a table that contained all of the module’s functions when I printed it. (also searched on the devforum for this problem, but couldn’t find anything)
local module = {}
local pathfindingservice = game:GetService("PathfindingService")
local bloodtable = {}
module.UpdateBloodQueue = function(details)
table.insert(bloodtable,1,details)
end
module.GetBloodQueue = function()
return bloodtable
end
module.ResetBloodQueue = function()
bloodtable = {}
end
module.UpdateTarget = function(npc,range,unabletotargettable,allyteam,isranged)
local target = nil
local lowestdistance = range
local function rangedunabletotarget(instancechecked) -- instancechecked = the model itself
local ignore = false
if isranged == true then
ignore = true
else
if table.find(unabletotargettable,instancechecked) then
ignore = nil
else
ignore = true
end
end
return ignore
end
for _,v in ipairs(game.Players:GetPlayers()) do
local check = v.Character.PrimaryPart ~= nil and rangedunabletotarget(v.Character) and v.Team ~= allyteam
if check then
local detectcheck
local distance = (v.Character.Torso.Position - npc.PrimaryPart.Position).Magnitude
detectcheck = distance < ((1 - v.Character.Torso.Transparency) * range) and distance < lowestdistance and v.Character.Humanoid.Health > 0
if detectcheck then
lowestdistance = distance
target = v.Character.Torso
end
end
end
for _,folderchild in ipairs(workspace.NPCs:GetChildren()) do -- searches through npc folder
if folderchild ~= npc.Parent then
for _,v in ipairs(folderchild:GetChildren()) do
local check
check = v:IsA("Model") and v.PrimaryPart ~= nil and v.Humanoid.Health > 0 and rangedunabletotarget(v)
if check then
local check2
local distance = (npc.PrimaryPart.Position - v.PrimaryPart.Position).Magnitude
check2 = distance < ((1 - v.PrimaryPart.Transparency)* range) and distance < lowestdistance
if check2 then
lowestdistance = distance
target = v.PrimaryPart
end
end
end
end
end
for _,v in ipairs(workspace.CapturableAreas:GetChildren()) do -- will detect all bases regardless of distance
if v.TeamCaptured.Value ~= npc.Parent and not table.find(unabletotargettable,v) then
local distance = (npc.PrimaryPart.Position - v.PrimaryPart.Position).Magnitude
if distance < lowestdistance then
lowestdistance = distance
target = v.PrimaryPart
end
end
end
return target
end
module.MakePath = function(npc,target,ranged,sightrange,raycastparam)
local cantarget = true
print(npc,target,ranged,sightrange)
local raycast = workspace:Raycast(npc.PrimaryPart.Position - Vector3.new(0,1,0),CFrame.new(npc.PrimaryPart.Position - Vector3.new(0,1,0),target.Position).lookVector * (8 * npc.Humanoid.WalkSpeed),raycastparam)
local raycheck = raycast and (raycast.Instance.Parent == target.Parent or raycast.Instance.Parent.Parent == target.Parent) and target.Parent.Humanoid.Health > 0
if raycheck then -- moves directly to target
local check
check = target ~= nil and target.Parent ~= nil
if ranged then -- if npc has ranged weapons then it will stay away a little
check = target ~= nil and target.Parent ~= nil and (npc.PrimaryPart.Position - target.Position).Magnitude > 50
end
if check then
local movingcframe = nil
movingcframe = CFrame.new(npc.PrimaryPart.Position,target.Position)
if (npc.PrimaryPart.Position - target.Position).Magnitude > (npc.Humanoid.WalkSpeed * 8) / 3 then -- responsible for zig-zagging if the npc is further than melee range
movingcframe = CFrame.new(npc.PrimaryPart.Position,target.Position + Vector3.new(math.random(-10,10),0,math.random(-10,10)))
end
npc.Humanoid:Move(movingcframe.LookVector)
end
else
npc.Humanoid:MoveTo(npc.PrimaryPart.Position + npc.PrimaryPart.CFrame.LookVector * 1)
local path = pathfindingservice:CreatePath()
path:ComputeAsync(npc.PrimaryPart.Position,target.Position)
if path.Status == Enum.PathStatus.Success then
local pathon = 0
local blockedconnection = nil
local runconnection = nil
local connectionset = false
local function followpath ()
for num,v in ipairs(path:GetWaypoints()) do
if connectionset == false then connectionset = true
blockedconnection = path.Blocked:Connect(function(pathblockednum) -- disconnects after 1 try to to compute path yields fail
if pathblockednum >= pathon then -- if the npc's path ahead is blocked
path:ComputeAsync(npc.PrimaryPart.Position,target.Position)
if path.Status == Enum.PathStatus.Success then
followpath()
else
cantarget = false
blockedconnection:Disconnect()
end
end
end)
end
local ifcheck
ifcheck = target ~= nil and (npc.PrimaryPart.Position - target.Position).Magnitude < (1 - target.Parent.PrimaryPart.Transparency) * sightrange and blockedconnection.Connected == true
if ifcheck then
local ray = workspace:Raycast(npc.PrimaryPart.Position - Vector3.new(0,1,0),CFrame.new(npc.PrimaryPart.Position,target.Position).lookVector * (8 * npc.Humanoid.WalkSpeed),raycastparam)
local ifcheck2
ifcheck2 = ray and (ray.Instance.Parent == target.Parent or ray.Instance.Parent.Parent == target.Parent) and target.Parent.Humanoid.Health > 0
if ifcheck2 then -- npc can move towards target
break
else -- follow path
if v.Action == Enum.PathWaypointAction.Walk then
npc.Humanoid:MoveTo(v.Position)
npc.Humanoid.MoveToFinished:Wait()
else
npc.Humanoid.Jump = true
end
end
else
break
end
end
end
followpath()
else
end
end
return cantarget,target
end
module.PathfindToCapturePoint = function(npc,caparea)
local targetsize = caparea.Size
local returneditem = nil
if (npc.PrimaryPart.Position - caparea.Position).Magnitude > math.max(targetsize.X,targetsize.Y,targetsize.Z) or npc.PrimaryPart.Position.Y <= caparea.Position.Y then -- checks if npc is far from capture point or is too low
local basepath = pathfindingservice:CreatePath()
basepath:ComputeAsync(npc.PrimaryPart.Position,caparea.Position)
if basepath.Status == Enum.PathStatus.Success then
local pathon = 0
local connection = nil
local connectionset = false
local function followpath() -- this function is specifically for bases
for num,v in ipairs(basepath:GetWaypoints()) do
if connectionset == false then connectionset = true
connection = basepath.Blocked:Connect(function(blocknum)
if blocknum > pathon then
basepath:ComputeAsync(npc.PrimaryPart.Position,caparea.Position)
if basepath.Status == Enum.PathStatus.Success then
followpath()
else
returneditem = false
connection:Disconnect()
end
end
end)
end
local followcheck = caparea.Parent.Parent == workspace.CapturableAreas and caparea.Parent.TeamCaptured.Value ~= npc.Parent and connection.Connected == true
if followcheck then
if v.Action == Enum.PathWaypointAction.Walk then
npc.Humanoid:MoveTo(v.Position)
npc.Humanoid.MoveToFinished:Wait()
pathon = num
elseif v.Action == Enum.PathWaypointAction.Jump then
npc.Humanoid.Jump = true
end
else
break
end
end
end
followpath()
else
returneditem = false
end
else -- npc is near cap point (this code will make the npc unable to target cap area)
returneditem = false
for c = 1,10 do
if caparea.Parent.TeamCaptured.Value ~= npc.Parent then
local maxsize = math.max(targetsize.X,targetsize.Y,targetsize.Z)
npc.Humanoid:MoveTo(caparea.Position + Vector3.new(math.random(-maxsize,maxsize),1,math.random(-maxsize,maxsize)))
wait(1)
else
break
end
end
end
end
return module
Was there a thing about modulescripts I missed?