How to check for a value change while loop is running

Hey,

I got a problem. Im currently doing a robber arrest system with my npc pathfinding system.
So there is a proximityprompt inside the robber and when the player triggers it, there should be an animation. This is all working but, the pathfinding system has a while true do loop and it would break when the Robber gets arrested cuz he wont be able to walk any further.

So how could I check if the value gets true and then skip parts of the while loop?`

while true do
move
move
move – lets say the robber gets arrested while moving to this point (it could be any other point too), how could I now skip all the other moves and just do the NPC:Destroy()
move
move
NPC:Destroy()

The real problem is that if i do an if statement, the loop will only check it 1 time while running, but i need it to check every second.

well you can use the repeat until

local Arrested = false(make a function that when the proximity gets activated the value gets true)

repeat
move
wait(0.01)
untill Arrested == true

i hope that will help you!

1 Like

sorry make the wait couple of seconds so it gives time for the npc to reach the point

or you can do

while Arrested == false do
move
end
1 Like

You can use a flag variable to track whether the robber has been arrested or not. In your while loop, you can check the value of the flag and choose to skip the rest of the loop if it is true.

Here’s an example of how you could implement this:

local arrested = false

while true do if arrested then break end

move 
move 
move

if arrested then break end

move 
move 

NPC:Destroy() 

end

To set the flag to true, you can use a function like this:

function arrestRobber() arrested = true end

Then, in your proximity prompt event handler, you can call the arrestRobber function to set the flag to true and break out of the loop.