Function yielding while loop

gonna keep it simple; i have pathfinding functions like walking to random waypoints and chasing the player.
in a while loop, there is an if statement that checks if there is a target, then chase said target, else, walk to random waypoints.

the problem is that when the walktorandom functions starts the rest of the while loop stops, which means we cant check for a target since the while loop is stopped.

the walktorandom function yields because at the end of the function it waits until the path is finished.

basically: walk to random waypoint function yields while loop but the while loop is needed for the pathfinding and the walk to random function has to wait for path to finish

while true do
	task.wait(.1)
	local _Target = LookForTarget()
	if _Target then
		ChaseTarget(_Target)
	else
		WalkToWaypoints() --ONCE THIS IS CALLED, THIS WHILE LOOP WILL YIELD UNTIL PATH IS FINSIHED
	end
end
local function WalkToWaypoints()
	local point = points[math.random(1,#points)]
	Path:Run(point.Position)
	if FoundTarget then print('stop')  Path:Stop() return end
	Path.Reached:Wait()	
end

Does the print statement run? Why not use Humanoid:MoveTo()?

thats irrelevant but no because of what i stated above

i literally am, just using a pathfinding module

Also, just to clarify, it has an idle behaviour of walking to random waypoints when there is no target detected, and when there is, it’ll follow the target around?

if there is no target, walk randomly
if there is, chase them
you can literally just look at the code

                                               ⚠️ Warning ⚠️

I’m only trying to help

This code was made by AI

To solve this issue, you can either use a separate thread or coroutine for the pathfinding functions or you can use a flag variable to indicate if the pathfinding function is currently running.

For the separate thread or coroutine solution, you can use the spawn() function in Roblox to create a new thread or coroutine that runs the pathfinding functions concurrently with the main while loop. This way, the main while loop can continue to check for targets even while the pathfinding functions are running.

For the flag variable solution, you can set a flag variable to true when the pathfinding function starts running and set it to false when it finishes. Then, in the main while loop, you can check if the flag variable is true or false before running the pathfinding function. If the flag variable is true, it means the pathfinding function is currently running and you should skip it. If the flag variable is false, it means the pathfinding function is not currently running and you can run it.

Here is an example of the flag variable solution:

local pathfindingRunning = false

while true do
task.wait(.1)
local _Target = LookForTarget()
if _Target then
ChaseTarget(_Target)
elseif not pathfindingRunning then
pathfindingRunning = true
WalkToWaypoints() --ONCE THIS IS CALLED, THIS WHILE LOOP WILL NOT YIELD
end
end
local function WalkToWaypoints()
local point = points[math.random(1,#points)]
Path:Run(point.Position)
if FoundTarget then print('stop') Path:Stop() return end
Path.Reached:Wait()
pathfindingRunning = false
end```

:man_facepalming: :man_facepalming:
if i wanted an answer from an AI i would the ask AI (no, this obviously doesn’t work)

2 Likes

What is the Path variable? Mind if we can see your entire script (and possibly other related scripts?

1 Like

Maybe you can use task.spawn?

Not sure if you’ll get the results you’re expected but maybe it’ll work:

task.spawn(WalkToWaypoints)
1 Like

all the info you need is in the post, the path variable is SimplePath.New (im using SimplePath)

i can summarize the problem though: the Walk function yields the While loop, but if the While Loop is being Yielding all of the code doesnt run. If i add a task.Spawn or Coroutine, the loop isn’t Yielded but the Walk function is running multiple times which means its trying to go to multiple waypoints @majdTRM

function ChaseTarget(_Target)
– task.wait(.1)
print(“chasing target”)
end

function WalkToWaypoints()
local _Waypoints = {}
–FIND RANDOM WAYPOINT TO GO TO
local _NewWaypoint = _Waypoints[math.random(1, #_Waypoints)]
–YIELD WHILE MOVING TO WAYPOINT
task.wait(.1)
end
]]–

–TASK.WAIT(0.1)

–[[
–local _Time = os.clock()
local _Time = os.time()
local _Time2= _Time

while true do
–_Time2 = os.clock()
_Time2 = os.time()
if _Time2 - _Time >= 10 then
print(“10 seconds have passed”)
_Time = _Time2
end
task.wait(.1)
end
]]–

–[[
local _Time = os.clock()
local _Time2= _Time

–while true do
for i = 1, 10 do
_Time2 = os.clock()
if _Time2 - _Time >= 10 then
print(“10 seconds have passed”)
_Time = _Time2
end
task.wait(.1)
end
–end
]]–

–[[
local _Time = os.clock()
local _Time2= _Time

while true do
_Time2 = os.clock()
if _Time2 - _Time >= 10 then
print(“10 seconds have passed”)
_Time = _Time2
end
task.wait(.1)
end
]]–

–[[

– PRIORITY QUEUE

local _PriorityQueue = {}
local _PriorityQueue2 = {}
local _PriorityQueue3 = {}
local _PriorityQueue4 = {}
local _PriorityQueue5 = {}

local _PriorityQueueCount = 0
local _PriorityQueue2Count = 0
local _PriorityQueue3Count = 0
local _PriorityQueue4Count = 0
local _PriorityQueue5Count = 0

local function PriorityQueuesAdd( _PriorityQueue, _PriorityQueueCount, task )
ount = _PriorityQueueCount + 1
ount] = task

can you explain what this is tho