Help with making a door open at a certain time

Hi everyone!

I was making a Jailbreak inspired game! i made a keycard door using a tutorial, and i now wanted to switch over to making it so that the door locks at a certain time and unlocks at a certain time. This is where i ran into a problem.
Running the script below causes this error:
image

This is the part of the script that is causing the error.

local db= false
local open = script.Parent.Open
local timing = game.Lighting.TimeOfDay

while true do
if timing > 140000 then
open = true
print(“door is open, its withing opening time”)
elseif timing < 210000 then
open = false
print(“closed. opening hours are up”)
end
end

Some help would be appreciated!

For this who want the full script:
local db= false
local open = script.Parent.Open
local red = script.Parent.Door1.red
local red2 = script.Parent.Door2.red
local timing = game.Lighting.TimeOfDay

while true do
if timing > 140000 then
open = true
print(“door is open, its withing opening time”)
elseif timing < 210000 then
open = false
print(“closed. opening hours are up”)
end
end

script.Parent.Trigger.Touched:connect(function(p)
if db == false then
db = true
if p.Parent:FindFirstChild(“Card”) ~= nil then
if open.Value == false then
local f = script.Parent.Door1.PrimaryPart.CFrameCFrame.Angles(0,-math.rad(90),0)
local f2 = script.Parent.Door2.PrimaryPart.CFrame
CFrame.Angles(0,math.rad(90),0)
for i = 0,1,0.1 do
local cfm = script.Parent.Door1.PrimaryPart.CFrame:lerp(f,i)
local cfm2 = script.Parent.Door2.PrimaryPart.CFrame:lerp(f2,i)
script.Parent.Door1:SetPrimaryPartCFrame(cfm)
script.Parent.Door2:SetPrimaryPartCFrame(cfm2)
wait()
end
open.Value = true
red.BrickColor = BrickColor.new(“Bright green”)
red2.BrickColor = BrickColor.new(“Bright green”)
wait(3) – how long does the doors open
local f3 = script.Parent.Door1.PrimaryPart.CFrameCFrame.Angles(0,math.rad(90),0)
local f4 = script.Parent.Door2.PrimaryPart.CFrame
CFrame.Angles(0,-math.rad(90),0)
for i = 0,1,0.1 do
local cfm = script.Parent.Door1.PrimaryPart.CFrame:lerp(f3,i)
local cfm2 = script.Parent.Door2.PrimaryPart.CFrame:lerp(f4,i)
script.Parent.Door1:SetPrimaryPartCFrame(cfm)
script.Parent.Door2:SetPrimaryPartCFrame(cfm2)
wait()
end
open.Value = false
red.BrickColor = BrickColor.new(“Really red”)
red2.BrickColor = BrickColor.new(“Really red”)
end
else
print(“User has no card”)
end
db=false
end
end)

script.Parent.Trigger2.Touched:connect(function(p)
if db == false then
db = true
if p.Parent:FindFirstChild(“Card”) ~= nil then
if open.Value == false then
local f = script.Parent.Door1.PrimaryPart.CFrameCFrame.Angles(0,math.rad(90),0)
local f2 = script.Parent.Door2.PrimaryPart.CFrame
CFrame.Angles(0,-math.rad(90),0)
for i = 0,1,0.1 do
local cfm = script.Parent.Door1.PrimaryPart.CFrame:lerp(f,i)
local cfm2 = script.Parent.Door2.PrimaryPart.CFrame:lerp(f2,i)
script.Parent.Door1:SetPrimaryPartCFrame(cfm)
script.Parent.Door2:SetPrimaryPartCFrame(cfm2)
wait()
end
open.Value = true
red.BrickColor = BrickColor.new(“Bright green”)
red2.BrickColor = BrickColor.new(“Bright green”)
wait(3) – how long does the doors open
local f3 = script.Parent.Door1.PrimaryPart.CFrameCFrame.Angles(0,-math.rad(90),0)
local f4 = script.Parent.Door2.PrimaryPart.CFrame
CFrame.Angles(0,math.rad(90),0)
for i = 0,1,0.1 do
local cfm = script.Parent.Door1.PrimaryPart.CFrame:lerp(f3,i)
local cfm2 = script.Parent.Door2.PrimaryPart.CFrame:lerp(f4,i)
script.Parent.Door1:SetPrimaryPartCFrame(cfm)
script.Parent.Door2:SetPrimaryPartCFrame(cfm2)
wait()
end
open.Value = false
red.BrickColor = BrickColor.new(“Really red”)
red2.BrickColor = BrickColor.new(“Really red”)
end
else
print(“User has no card”)
end
db=false
end
end)

1 Like

This returns a string of what time of day it is, not a number.
image

A solution to this is to use game.Lighting.ClockTime instead. That would mean you have to change some other lines since it returns a smaller number:

local timing = game.Lighting

--From the lines where you compare the values
if timing.ClockTime > 14 then

Hope this helps

also, you should indent your code so its easier for people to read. Type a “```” between your code and it should be indented

4 Likes

Thanks! I’ll try this instead!

Hi again.
when doing this it simply gives the error:
image

1 Like

I see. After taking a second look at your script i can see you didnt put a wait() in your while loop. While loops need a yielding function (ex. wait()) so it doesnt time out the script. A fix for this would be to replace while true do with while wait() do.

1 Like