So i have a game were it selects a Map located in a folder in ServerStorage, clones it and Parent it to the Workspace. When that happens, a script inside a Map spawns AIs and these AIs haves Path finding. I'm having a problem were, when the map gets added to the workspace, it breaks the Path finding (Or my script itself). What i mean by that is that sometimes, the path just doesn't have a end point, so the character stays were he is jumping (Without any error in the output) and sometimes a part of the path just doesn't exist.
(For example:
- : path o : character x : end point o-------- -x)
Though i have made some tests and it seamed that when the Map is already in the Workspace, the AI worked perfectly. I tried to make the Map originally be located in Workspace, then it gets parented to the ServerStorage and then it gets added back to the Workspace and it also didn't worked. I also tried to instead of cloning it, just parent the Map and it also didn't worked.
So does path finding get affected if the Map's Parent gets changed or what?
Here's my AI script (Note: The place were the character moves to its end point is in the MoveToSpawnPoint() function at line 112):
-------------
--Variables--
-------------
local TC = nil
TC = script.Parent.Configuration.TeamColor.Value
--local pointModel = Instance.new("Model",game.Workspace)
local hum = script.Parent.Humanoid
local Pathfinding = game:GetService("PathfindingService")
-------------
--Functions--
-------------
--Finds the closest Enemy
function findClosestEnemy()
--Variables
local p = game.Players:GetChildren()
local closestEnemy = nil
local maxDistance = 50
--Add NPCS to the players table
for _, v in pairs (script.Parent.Parent:GetChildren()) do
if v.Name == "Noob" or v.Name == "Guest" then
table.insert(p,v)
end
end
for _,player in pairs (p) do
--If "player" classname is a player then
if player.ClassName == "Player" then
if player.TeamColor ~= TC then
local char = player.Character
if char ~= nil then
local humanoid = char:findFirstChild("Humanoid")
if humanoid and humanoid.Health>0 then
if closestEnemy == nil and (char.Torso.Position - script.Parent.Torso.Position).magnitude<maxDistance then
closestEnemy = char
elseif (char.Torso.Position - script.Parent.Torso.Position).magnitude<maxDistance then
local NCEMag = (char.Torso.Position - script.Parent.Torso.Position).magnitude
local CEMag = (closestEnemy.Torso.Position - script.Parent.Torso.Position).magnitude
if NCEMag<CEMag then
closestEnemy = char
end
end
end
end
end
--If "player" classname is not a player then
else
if player ~= nil and player ~= script.Parent then
if player.Configuration.TeamColor.Value ~= TC then
local humanoid = player:findFirstChild("Humanoid")
if humanoid and humanoid.Health>0 then
if closestEnemy == nil and (player.Torso.Position - script.Parent.Torso.Position).magnitude<maxDistance then
closestEnemy = player
elseif (player.Torso.Position - script.Parent.Torso.Position).magnitude<maxDistance then
local NCEMag = (player.Torso.Position - script.Parent.Torso.Position).magnitude
local CEMag = (closestEnemy.Torso.Position - script.Parent.Torso.Position).magnitude
if NCEMag<CEMag then
closestEnemy = player
end
end
end
end
end
end
end
return closestEnemy
end
--Finds the closest Enemy SpawnPoint
function findClosestEnemySpawnpoint()
local SpawnsContainer = script.Parent.Parent:GetChildren()
local closestSpawnPoint = nil
for _, spawn in pairs (SpawnsContainer) do
if spawn.ClassName == "SpawnLocation" then
if spawn.TeamColor ~= TC then
if closestSpawnPoint == nil then
closestSpawnPoint = spawn
else
local NSPMag = (spawn.Position - script.Parent.Torso.Position).magnitude
local CSPMag = (closestSpawnPoint.Position - script.Parent.Torso.Position).magnitude
if NSPMag < CSPMag then
closestSpawnPoint = spawn
end
end
end
end
end
return closestSpawnPoint
end
--Visualizes the path (only for testing)
function visualizePath(path)
-- clear old path visualization
for _, point in pairs(pointModel:GetChildren()) do
point:Destroy()
end
-- calculate new visualization
for _, point in pairs(path) do
local part = Instance.new("Part")
part.Parent = pointModel
part.FormFactor = Enum.FormFactor.Custom
part.Size = Vector3.new(1,1,1)
part.Position = point
part.Anchored = true
part.CanCollide = false
end
end
--Move to SpawnPoint
function MoveToSpawnPoint(s)
local PF = Pathfinding:ComputeSmoothPathAsync(script.Parent.Torso.Position,s.Position,512)
local points = PF:GetPointCoordinates()
--visualizePath(points)
local distance;
local br = false
for i = 1,#points do
wait(0)
if br then break end
local count = 0
local E = findClosestEnemy()
if E == nil then
repeat
count=count+1
if count>20 then
br = true
break
end
hum:MoveTo(points[math.min(#points,i+1)])
if points[i].Y>script.Parent.Torso.Position.Y+3 then
hum.Jump = true
end
distance=(points[i]*Vector3.new(1,0,1)-script.Parent.Torso.Position*Vector3.new(1,0,1)).magnitude
wait()
until distance<5
else
break
end
end
end
--Move to Enemy
function MoveToEnemy(e)
local PF = Pathfinding:ComputeRawPathAsync(script.Parent.Torso.Position,e.Torso.Position,50)
local points = PF:GetPointCoordinates()
--visualizePath(points)
local distance;
local br = false
for i = 1,#points do
wait(0)
if br then break end
local count = 0
repeat
count=count+1
if count>20 then
br = true
break
end
hum:MoveTo(points[math.min(#points,i+1)])
distance=(points[i]*Vector3.new(1,0,1)-script.Parent.Torso.Position*Vector3.new(1,0,1)).magnitude
wait()
until distance<10 or e.Humanoid.Health<=0 or hum.Health<=0 or (script.Parent.Torso.Position-e.Torso.Position).magnitude <10
if (script.Parent.Torso.Position-e.Torso.Position).magnitude <10 and e.Humanoid.Health>0 and hum.Health>0 then
hum:MoveTo(e.Torso.Position,e.Torso)
end
if e.Humanoid.Health<=0 or hum.Health<=0 then
script.Parent.Target.Value = nil
return
end
end
end
-------------
--Task Area--
-------------
while wait(0) do
--Finds if there is any enemy close
local Enemy = findClosestEnemy()
if Enemy~= nil then
script.Parent.Target.Value = Enemy
MoveToEnemy(Enemy)
--If theres not any enemy close, then go to the spawn points
else
script.Parent.Target.Value = nil
local Spawn = findClosestEnemySpawnpoint()
if Spawn ~= nil then
MoveToSpawnPoint(Spawn)
end
end
end
