Currently, I’m trying to get models or spawned mobs that go to a specific location(part) after spawning. When spawned all the mobs should head to the set location. Then after 30 seconds of the original mobs spawning another set of mobs should be spawned and go to that same location set and so on.
As of right now when spawned only the first minion that was cloned and put into the minion folder will pathfind and move to the set location. Then another set of mobs will spawn 30 seconds after mob reaches its destination instead of when it spawned. But when the new pair of mobs spawn not even the first one cloned will move after the first set of mobs.
Originally I tried making a looping without an array and got an error. Then when I put a loop in with an array I was able to get rid of the error. However, it’s not working how intended.
local GAME_ACTIVE = true
local iceminion = game.ServerStorage.Minions["IceMin"]
-- Spawn Location One
local spawnA1 = Vector3.new(101, 3, -268)
local spawnA2 = Vector3.new(101, 3, -271.5)
while GAME_ACTIVE == true do
local cIce = iceminion:Clone()
local cIce1 = iceminion:Clone()
--local cIce2 = iceminion:Clone()
cIce.Parent = game.Workspace["Minions"]
cIce:MoveTo(spawnA1)
cIce1.Parent = game.Workspace["Minions"]
--cIce1:MoveTo(spawnA2)
local minionfolder = game.Workspace["Minions"]
--local = path["IceMin"]
local pathArray = {
minionfolder.IceMin,
minionfolder.IceMin
}
local PathfindingService = game:GetService("PathfindingService")
local IceMin = game.Workspace.Minions["IceMin"]
local humanoid = IceMin.Humanoid
local destination = game.Workspace.location1
local destination1 = game.Workspace.location2
for MinIndex = 1, #pathArray do
local path = PathfindingService:CreatePath()
path:ComputeAsync(IceMin.HumanoidRootPart.Position, destination.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
humanoid:MoveTo(waypoint.Position)
-- Wait until part has reached the waypoint before continuing
humanoid.MoveToFinished:Wait()
path:ComputeAsync(IceMin.HumanoidRootPart.Position, destination1.Position)
end
end
wait(30) -- Wait seconds.
end
Not sure if an array is the best way or not to do this. What’s the best way I can accomplish this?
I ended up changing the last half to this and get no errors, but still doesn’t work. However it now creates a path, just doesn’t work with waypoints.
local IceMin = game.Workspace.Minions:GetChildren()
for MinIndex = 1, #IceMin do
local path = PathfindingService:CreatePath()
path:ComputeAsync(IceMin[MinIndex].HumanoidRootPart.Position, destination.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
IceMin[MinIndex].Humanoid:MoveTo(waypoint.Position)
-- Wait until part has reached the waypoint before continuing
IceMin[MinIndex].Humanoid.MoveToFinished:Wait()
path:ComputeAsync(IceMin[MinIndex].HumanoidRootPart.Position, destination1.Position)
It would probably be easier for you to create a script with the pathfinding code into the minion model.
Then each time it gets cloned it will run the script for that clone to follow the path.
At this point, none of the code is going to run until the first mob finishes it’s move. The most basic solution to get around this is wrapping the pathfinding logic for each mob inside of a coroutine.wrap. However, this could theoretically cause lag depending on the number of mobs. There are probably better solutions for mass pathfinding, but this is the simplest way to fix your current code.
So I did what you said and put the script in the model, but the script only runs the first time when the minion is spawned for some reason?
local IceMin = script.Parent
local GAME_ACTIVE = true
local PathfindingService = game:GetService("PathfindingService")
local humanoid = IceMin.Humanoid
local destination = game.Workspace.TurretA1
local destination1 = game.Workspace.TurretA2
while GAME_ACTIVE == true do
local path = PathfindingService:CreatePath()
path:ComputeAsync(IceMin.HumanoidRootPart.Position, destination.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
-- Wait until part has reached the waypoint before continuing
humanoid.MoveToFinished:Wait()
path:ComputeAsync(IceMin.HumanoidRootPart.Position, destination1.Position)
end
end
Here’s the serversided script which does spawn them.
local GAME_ACTIVE = true
local iceminion = game.ServerStorage.Minions[“IceMin”]
local spawnA1 = Vector3.new(101, 3, -268)
while GAME_ACTIVE == true do
for i = 1, 6, 1 do
local cIce = iceminion:Clone()
cIce.Parent = game.Workspace["Minions"]
cIce:MoveTo(spawnA1)
wait(1)
end
wait(30) -- Wait seconds.
end
I’ve modified your script a little, copy/paste this into the minion script:
local IceMin = script.Parent
local PathfindingService = game:GetService("PathfindingService")
local humanoid = IceMin.Humanoid
local destinations={game.Workspace.TurretA1, game.Workspace.TurretA2} -- the list of parts that make up the path to follow
local nextDestination=1
local waypoints
local nextWaypoint
local function ShowWaypoints(waypoints) -- You can delete this function if not required
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.3, 0.3, 0.3)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.CanTouch = false
part.Parent = workspace.waypoints -- Create a folder in workspace called waypoints to store these
end
end
local function CreatePath()
local path = PathfindingService:CreatePath()
path:ComputeAsync(IceMin.HumanoidRootPart.Position, destinations[nextDestination].Position)
if path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
ShowWaypoints(waypoints) -- Delete this line if you don't want to display the waypoints
nextWaypoint=1
humanoid:MoveTo(waypoints[nextWaypoint].Position)
end
end
local function onWaypointReached(reached)
if reached and nextWaypoint < #waypoints then
nextWaypoint+=1
humanoid:MoveTo(waypoints[nextWaypoint].Position)
else
nextDestination+=1
if nextDestination>#destinations then
print("Reached Final Destination.") -- Change this to whatever you want the minions to do after it completes the path
else
CreatePath() --Move to the next part in the list
end
end
end
humanoid.MoveToFinished:Connect(onWaypointReached)
CreatePath()
Also, change the following line from the server script that clones the minions:
cIce:MoveTo(spawnA1)
to
cIce:SetPrimaryPartCFrame(CFrame.new(spawnA1)) -- Move the minion to it's spawn point
I think my game might be glitched, only the first one moves. Then I noticed if I put wait in the server script for it spawning it will spawn, but not even move the first one.
I tried starting a new project file and it didn’t work. I got rid of the terrain and had it on top of a part and that somehow got it to work. However, it goes directly to the second object. I changed up destinations order and it seems to always go to the last object. It’s also not taking the shortest path which is weird, I think it’s some terrain bug/issue or something.
Terrain can sometimes cause issues with Pathfinding. There is an option in Studio Settings that you can enable which shows the pathfinding mesh (the option is called ‘Show Navigation Mesh’). If you enable this and discover that the navigation mesh is actually way underneath your terrain then there is probably a rogue piece of terrain some where that needs to be deleted.
Thanks! I edited the option, not seeing any issues. Not even the first minion is moving anymore. This is so weird. I’ll see if I can find a solution on the form.