Running a function while the other returns false

I’m currently scripting an enemy system for my project and have came up to this obstacle that I can’t think of an efficient way to come over it.
Basically, the enemy NPC is supposed to be roaming around until a player gets in its radius. Once the player gets in, the NPC goes after it, below is my sketch of how it is supposed to run.
I’m using a handler to handle the functions in the correct sequence to achieve the required outcome.

Sorry for the unclear image!

All these modules are scripted efficiently and work perfectly.
However I can’t figure a way to make the handler run the Roam function while there is no players in radius.
The FindPlr function:

return {
function()
	local Pos = HumRoot.Position
	local NearestChar
	for i = 1, #CharList do
		if (HumRoot.Position - Pos).magnitude < Data.Range then
			Data.Range = (HumRoot.Position - Pos).magnitude
			NearestChar = CharList[i]
		end
	end
	return NearestChar
end
}

And the roaming is just a ray, casted onto the ground to find a spot then uses Humanoid:MoveTo(Point)

I believe coroutines may help you do this.
You can kick one off and later on pause it or resume it.

1 Like