While .. Do-loop doesn't run after switching off and on

Hello Forum.

To get straight to the point, I have two separate scripts, one which recognizes if the player entered an area, and one which does the opposite – if the player leaves the area, both are ServerScripts, bound by a BindableEvent. To check if the player is in the area, a loop runs to check, but after wanting to enable it the second time, it seems to cease.

PlayerInArea
When the player is in the area, a While .. Do-loop is being played to constantly check if a player is in the area marked by a part. The loop works fine, up until I leave the area. Because after the debounce has been set to false a second time, it stops showing the print("Working"), despite the print from the BindableEvent below it does work; saying the debounce is false.

Loop in question
local debounce = false

-- function with all the code checking for player

while debounce == false do --loop is disconnected from the function above
	GetPlayersInPart(game.Workspace.Detector)
	print("Working") --this works at first
	task.wait(0.25)
end

game.ReplicatedStorage.CooldownFalse.Event:Connect(function() --will explain in next header
	debounce = false
	print(debounce) --returns the print 'false', which works
end)

PlayerLeaveArea
When the player leaves the area, they walk through a ‘forcefield’ like part I surrounded the area with, which will play the code in the detail. This will in fact fire a BindableEvent back to the PlayerInArea and it will give the right results.

Code when player leaves
local debounce = false

--function

   game.ReplicatedStorage.CooldownFalse:Fire() --fires back to the PlayerInArea, works

game.ReplicatedStorage.CooldownTrue.Event:Connect(function() --outside of function above
	debounce = true
	print(debounce) --also prints the right debounce
end)

The issue? The loop will not run again even though the conditions of debounce == false are met.
Is there a different way to re-enable the loop? Am I forgetting to add something? Please let me know or ask if you require more information.

Thank you for reading and for any help in advance.

1 Like

This will not run ever again since it disconnects itself and doesn’t reconnect anymore. You can just use a while true loop then check if debounce is false (NOTE: This will prevent any code below from running as it yields, it is best to move it to the very bottom of the script)

while true do --loop is disconnected from the function above
task.wait()
if debounce == false then -- check if debounce is false
GetPlayersInPart(game.Workspace.Detector)
	print("Working") --this works at first
	task.wait(0.25)
end
end
2 Likes

Your code sample will cause the script to timeout btw. You only have a wait() statement within the if statement, so if the if statement isn’t true then it just crashes the script.

Aside from that, the reasoning is correct

2 Likes

Oh right I forgot about that, I fixed the script

You just need to change your loop to this:

while task.wait(.25) do
	if debounce == false then continue end

	GetPlayersInPart(game.Workspace.Detector)
	print("Working")
end

This makes the wait as the condition which is always true and checks for debounce inside the loop, if it’s false it just skips the current iteration and does nothing.

Apologies for the late response, but I was not able to recreate my script as some things got scrambled in the making of. However, this rendered to be the solution.

Thank you for the assistance, likewise to the others who replied to my topic.

1 Like

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