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.
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
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
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
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.