If statement not working when variable is true

I have an idling cycle that happens but I need that when the clickDetector is detected it interrupts the cycle and performs something else.

What I thought would happen is that when playerInput is true, the first if statement in the bottom while loop somewhat gets reset and will see that playInput is true and fire the second if statement that is inside it (print for demonstration)

local sound01 = game.SoundService.Sounds.sound01

local Detect = game.Workspace.TEMP.ClickDetector
local playerInput = false
local idleBegin = false

Detect.MouseClick:Connect(function()
	playerInput = true
end)

spawn(function()
	while true do
		wait()
		if playerInput == true then
			moduleScript.idleEnable = false
			moduleScript.idleEnable = true --resets the if moduleScript.idleEnable == true statement (atleast im pretty sure it does)
			repeat wait() until moduleScript.idleEnable == false
		end
	end
end)

while true do
	wait()
	if moduleScript.idleEnable == true then
		if playerInput == true then		
			print("working") --doesnt print 
			repeat wait() until moduleScript.idleEnable == false
		end
		wait(4)
		sound01:Play()
		Detect.MaxActivationDistance = 0
		sound01.Ended:Wait()
		idleBegin = true
		repeat wait() until moduleScript.idleEnable == false
	else
		idleBegin = false
		Detect.MaxActivationDistance = 150
		repeat wait() until moduleScript.idleEnable == true
	end
end

This is all inside a LocalScript that is in StarterGui as I have ImageButtons that have functions which disable idleEnable in the beginning before the function and re-enable idleEnable at the end of the function.

I am using a moduleScript for idleEnable so that other scripts can enable or disable the idle cycle.

The moduleScript has idleEnable set to true.

idleBegin is connected to another if statement inside a spawn while loop above that I didn’t show as that part works fine.

did you try to add a print in the if statement?

It seems to me that your problem is at

Detect.MouseClick:Connect(function()
	playerInput = true
end)

Try

Mouse.Button1Down:Connect(function()
	print("Clicked")
	playerInput = true
end)

and see if that prints.
Also, module scripts only run once and so, connects may not work at all in this type of script, but I am not entirely sure.

I tried putting a print after the else just now and it performed right after the if playerInput == true statement when idleEnable went false. No idea what is happening there.

I’ve found out that the wait(4) in the second if statement is somehow stopping the third if statement from running when the loop is “reset”. When I put else continue in the third if statement then the print(“worked”) will run when playerInput is true. The issue is this also stops the functions in the 2nd if statement from running while playerInput is false.
Am very confused on all this.