How to wait until a module script function stops

Hello everyone, I have an issue. I need to wait for a modulescript function to stop, and I tried doing this by returning then using repeat task.wait() until func == true. For some reason, this isnt working. Any help?

Script

local thing = mod.Path(script.Parent)
repeat task.wait(.2) until thing == "Zoo"

Module script

print("Got to the return statement")
return true

I know it is getting the the return because the print prints. I also printed ‘thing’ in the repeat loop, and it repeated ‘nil’ forever.

3 Likes

Hi,
I have noticed that your code is a bit vague, making it hard to understand. Is there any way you could provide more context?

2 Likes

Sure, this is a pathfinding script.

Script

while true do 
	local mod = require(game.ServerScriptService.PathFind)
	local thing = mod.Path(script.Parent)
	repeat task.wait(.2) until thing == true
	task.wait(2)
end```

module script (when the npc is done moving)

connection = npc.Humanoid.MoveToFinished:Connect(function(reached)
						if reached and currentIndex < #waypoints then
							currentIndex += 1
							npc.Humanoid:MoveTo(waypoints[currentIndex].Position)
							if waypoints[currentIndex].Action == Enum.PathWaypointAction.Jump then
								npc.Humanoid.Jump = true
							end
						else
							print("Disconnection connection")
							connection:Disconnect()
							connection = nil
							pathBlockedConnection:Disconnect()
							pathBlockedConnection = nil
							print("Got to the return statement")
							return true
						end

there

1 Like

You are returning true inside of another function, which is connected to npc.Humanoid.MoveToFinished.

1 Like

Ahh, so what is a workaround? dbdfbdfb

1 Like

Adding onto what Green said, I would put return true outside of the “connection” function.

By the way, is the module script performing the actions inside of it?

1 Like

Yes, that is why I am confused on how else to do it. I can send the whole thing if needed.

1 Like

Sure, please do.

Character Limit ___________________

1 Like
local PathFindingService = game:GetService("PathfindingService")

local NODES = game:GetService("Workspace").NODES

local pathfinding = {}

pathfinding.Path = function(npc)
	local randomNode = nil
	local success = false
	
	repeat 
		randomNode = NODES:GetChildren()[math.random(1, #NODES:GetChildren())]
		local partsIn = game.Workspace:GetPartsInPart(randomNode)
		for i, v in pairs(partsIn) do
			if v.Parent == npc then
				success = false
				continue
			end
		end

		success = true
	until success == true
	
	local function walk()
		
		local path:Path
		local AgentParameters = {
			WaypointSpacing = 4,
			Costs = {
				Concrete = 0.1 
				
			}
		}
		path = PathFindingService:CreatePath(AgentParameters)
			
		local humanoidRootPart = npc:WaitForChild("HumanoidRootPart")
		
		local connection 
		local pathBlockedConnection

		local RETRY_NUM = 0
		local success,errormessage

		repeat 
			RETRY_NUM += 1
			success, errormessage = pcall(path.ComputeAsync, path, humanoidRootPart.Position, randomNode.Position)
			if not success then 
				task.wait(.5)
			end
		until success or RETRY_NUM > 5

		if success then
			if path.Status == Enum.PathStatus.Success then
				local waypoints = path:GetWaypoints()
				local currentIndex = 2

				if not connection then
					connection = npc.Humanoid.MoveToFinished:Connect(function(reached)
						if reached and currentIndex < #waypoints then
							currentIndex += 1
							npc.Humanoid:MoveTo(waypoints[currentIndex].Position)
							if waypoints[currentIndex].Action == Enum.PathWaypointAction.Jump then
								npc.Humanoid.Jump = true
							end
						else
							print("Disconnection connection")
							connection:Disconnect()
							connection = nil
							pathBlockedConnection:Disconnect()
							pathBlockedConnection = nil
							print("Got to the return statement")
							return true
						end
					end)

					pathBlockedConnection = path.Blocked:Connect(function(waypointNumber)
						if waypointNumber > currentIndex then
							print("Disconnection path block")
							connection:Disconnect()
							pathBlockedConnection:Disconnect()
							connection = nil
							pathBlockedConnection = nil
							walk()
						end
					end)
				else
					print("Connection exists")
				end
				
				npc.Humanoid:MoveTo(waypoints[currentIndex].Position)
				
				if waypoints[currentIndex].Action == Enum.PathWaypointAction.Jump then
					npc.Humanoid.Jump = true
				end
			else
				warn("Path was not a success")
			end
		else
			warn("Path computing error", errormessage)
			return
		end
	end
	
	walk()
end

return pathfinding

You could maybe try this?

while currentIndex < #waypoints do
	local reached = npc.Humanoid.MoveToFinished:Wait()
	if not reached then return false end
	
	currentIndex += 1
	npc.Humanoid:MoveTo(waypoints[currentIndex].Position)
	if waypoints[currentIndex].Action == Enum.PathWaypointAction.Jump then
		npc.Humanoid.Jump = true
	end
end

print("Disconnection connection")
pathBlockedConnection:Disconnect()
pathBlockedConnection = nil
print("Got to the return statement")
return true
1 Like

that isnt really what i want i dont think

Then what are you looking for? As I understood it, you wanted the function to yield until the NPC has finished moving. It is what this code would do.

Thanks,

I would create a local variable called IsPathfindSuccessful, and instead of doing return true or return false, I would set the local variable to the boolean that you were returning, and then at the end of the function (underneath walk()), return IsPathfindSuccessful.

For example, you would change return true to IsPathfindSuccessful = true and then at the end of the function, below walk(), you would return IsPathfindSuccessful.

If you have any questions or queries please do feel free to ask.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.