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.
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.
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)
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)