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.