Help with miner npcs targetting the same ore


I still run into the issue of half of the miners just being idle and the other half wandering around aimlessly.

May be good to add CollectionService:RemoveTag(nearest, "ToBeMined") when they are hit inside breakOre. There is certainly some part here that needs the tag removed, maybe if walking fails, think of ToBeMined as a debounce to be toggled when selecting and when broken/errored.

1 Like

I was about to say stuff about me deleting the model later on in the script but I added that in and it actually did work, yet still have this issue sadly:


would it be wise to pcall it as I said in a previous reply? or is this avoidable somehow.

I feel like it is something to do with this line of code
image
and definitely this line of code, as the error message calls this as being the issue

Sorry! Yeah I am not sure why that would be happening, is Main a part of all Stone? which line is that?

1 Like

Oh I should’ve maybe mentioned this at the start, but here is how a single ore model looks like, all of them are identical apart from their values and the actual name of the model.
image
Edit: Main is the, well, main part of the ore, the stone/slate mesh thing in the centre.

Well I certainly do not know why that is happening then, if nearest == nil or nearest.Main == nil then should capture that problem. I say try GetPivot instead, it works with both models and parts, maybe the space between nearest and .Main i really don’t know lol.

(humrp.Position - nearest:GetPivot().Position).Magnitude <= 10

Maybe nearest is destroyed at that point after walking takes some time, since :Destroy only sets and locks the parent to nil we should check that too

if nearest and nearest.Parent and (humrp.Position - nearest:GetPivot().Position).Magnitude <= 10 then
1 Like

Wow that actually somehow fixed all of the problems I had before, thanks, I will do a stress test on this and see if it breaks.

1 Like

Man this post is going on for far too long, i’ve found a few issues but no error messages for them:


Sometimes, the miners just stop completely as if the script has been removed from them and through some print statements, i’ve figured out that this code stops them:

			if nearest == nil or nearest.Main == nil then
				return
			end

I tried to make this call the walkToOre statement again when it returns but that just ended in a stack overflow and studio nearly crashing.

Edit: I just changed the return to nearest = getNearestOre(), thanks for all the help man!

Edit2: the moment I typed the first edit in I checked studio and saw this:
https://gyazo.com/6069f96223d72c026202b6ef7dc4eca4
i’m also definitely not playing Unit: Classified [TECH DEMO] in the background.

@gertkeno I now put a print in
image
this and
image
pain.

You should probably do, The wait is important like it is in the while loop at the end of walkToOre, speaking of which you should remove that while loop. The function is already recursive and will degrade performance over time if you loop and recurse at the same time.

if nearest == nil or nearest.Main == nil then
	task.wait(0.5)
	walkToOre()
	return -- still important! return prevents multiple runs while recursing
end

-- later on ...
WalkAnim:Stop()
-- while task.wait(0.5) do -- delete this
task.wait(0.5)
walkToOre()
1 Like

image
still just inactivating after a while, if I put a print in between the task.wait(0.5) walkToOre() return then it prints nothing throughout the entire time, would you like a place download?

sure I’ll take a place download. It should be printing nothing as if nearest == nil then should be the one catching this, it’s the case where it cannot find a closest ore that isn’t tagged.

– place download link removed to not let anyone else reading this to copy my game
edit: miner script is located in ServerScriptService → AI.

I think this addition will fix them idling around, I added remove tag after the pathfinding, since it will only try to break ore when they are close enough. Another fix may be to increase the minimum distance but then the teleport is quite visible. I also removed the MiningAnim.Stopped:Wait() as that never seemed to trigger for me.

			if success and path.Status == Enum.PathStatus.Success then
				local waypoints = path:GetWaypoints()
				for _,waypoint in pairs(waypoints) do

					if waypoint.Action == Enum.PathWaypointAction.Jump then
						hum:ChangeState(Enum.HumanoidStateType.Jumping)
					end

					if nearest and nearest.Parent and (humrp.Position - nearest:GetPivot().Position).Magnitude <= 10 then
						breakOre(nearest)
					end

					hum:MoveTo(waypoint.Position)
					hum.MoveToFinished:Wait()
				end
			end
			
			-- removes tag if failed to mine
			if nearest and nearest.Parent then
				CollectionService:RemoveTag(nearest, "ToBeMined")
			end

			WalkAnim:Stop()
			task.wait(0.5)
			walkToOre()
		end
1 Like

Yep, thank you so much for the help, here are the results after stress testing for half an hour


The one slight bug feature that i’ve spotted is that they occasionally stop for like 20 seconds or flip over but those are manageable and look kinda goofy, i’ll work them out myself or even keep them.

1 Like