While loop not playing even though the requirement is met?

I am making a door script and it works fine. Hovewer I wanted it to close again for a few seconds, hovewer, the loop that is supposed to run when it is opened, does not run. Here is my script:

local tween = game:GetService("TweenService")

local center = script.Parent.Center
local opened = false

local CF = Instance.new("CFrameValue")
CF.Value = center.CFrame

CF:GetPropertyChangedSignal("Value"):Connect(function()
	
	center.CFrame = CF.Value
	
end)

local debounce = false

local last

script.Parent.DoorHandle.ClickDetector.MouseClick:Connect(function()
	
	if debounce == true then return end
	
	debounce = true
	
	if opened == false then
		
		tween:Create(CF, TweenInfo.new(1, Enum.EasingStyle.Quart), {Value = center.CFrame * CFrame.Angles(math.rad(0),math.rad(90),math.rad(0))}):Play()
		script.Parent.Center.DoorClose:Play()
		opened = true
		last = tick()
		
	else
		
		tween:Create(CF, TweenInfo.new(1, Enum.EasingStyle.Quart), {Value = center.CFrame * CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))}):Play()
		script.Parent.Center.DoorOpen:Play()
		opened = false
		last = nil
		
	end
	
	wait(1)
	debounce = false
	
end)

while opened do
	
	wait()
	
	print("looping") -- this does not print
	
	if not last or not opened then 
		
	else
		
		print(last - tick())	
		
		if last - tick() >= 5 then
			
			print("last!")
			tween:Create(CF, TweenInfo.new(1, Enum.EasingStyle.Quart), {Value = center.CFrame * CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))}):play()
			script.Parent.Center.DoorOpen:Play()
			opened = false
			last = nil
			break
			
		else
			
			last = last - 1
			wait(1)
			
		end
		
	end
		
end

I have added a print statement which should print every loop, but it does not.
Any help is appreciated.

2 Likes

Its because the while loop happens after the server ever was created. Events do not prevent things after the end) of happening.

3 Likes

So it’s the same as putting it before the event, which means it will yield the script until the loop is done?

1 Like

here, lets put it this way. Because the while loop is outside of the function that changes the open variable, it does not run because it checks when the server is first created and never again.

2 Likes

Basically in your case, the event was indeed registered and will do the changes however, the loop started running once the server started because the loop is outside the event (same thing as outside the functions)

1 Like

(@AstralBlu_e) So I should get that loop to run inside the event?

1 Like

Yes, I think you should use coroutine for it, example:

coroutine.wrap(function()
while opened do

end

end)()

Just so it can not interrupt the other codes of that script of running.

2 Likes

I’ve put the loop inside the event:

local tween = game:GetService("TweenService")

local center = script.Parent.Center
local opened = false

local CF = Instance.new("CFrameValue")
CF.Value = center.CFrame

CF:GetPropertyChangedSignal("Value"):Connect(function()
	
	center.CFrame = CF.Value
	
end)

local debounce = false

local last

script.Parent.DoorHandle.ClickDetector.MouseClick:Connect(function()
	
	if debounce == true then return end
	
	debounce = true
	
	if opened == false then
		
		tween:Create(CF, TweenInfo.new(1, Enum.EasingStyle.Quart), {Value = center.CFrame * CFrame.Angles(math.rad(0),math.rad(90),math.rad(0))}):Play()
		script.Parent.Center.DoorClose:Play()
		opened = true
		last = tick()
		
	else
		
		tween:Create(CF, TweenInfo.new(1, Enum.EasingStyle.Quart), {Value = center.CFrame * CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))}):Play()
		script.Parent.Center.DoorOpen:Play()
		opened = false
		last = nil
		
	end
	
	wait(1)
	
	debounce = false
	
	while opened do
	
		wait()
		
		print("looping")
		
		if not last or not opened then 
			
		else
			
			print(math.abs(last - tick()))	
			
			if math.abs(last - tick()) >= 10 then
				
				print("last!")
				tween:Create(CF, TweenInfo.new(1, Enum.EasingStyle.Quart), {Value = center.CFrame * CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))}):play()
				script.Parent.Center.DoorOpen:Play()
				opened = false
				last = nil
				break
				
			else
				
				last = last - 1
				wait(1)
				
			end
			
		end
			
	end
	
end)

Thanks for the help!

2 Likes