Hello! So I was making a gate to where it opened and closed at a certain time(In game). I have been trying to fix this issue for the past week but couldn’t find out what was wrong with my script. Could anyone help me with this? I am not sure if I have missed an error or if there is something wrong with the scripts that I have.
DayNightModule script
local Lighting = game:GetService("Lighting")
-- constants
local TIME_SPEED = 60 -- 1 min = 1 hour
local START_TIME = 9 -- 9am
local minutesAfterMidnight = START_TIME * 60
local waitTime = 60 / TIME_SPEED
while true do
minutesAfterMidnight = minutesAfterMidnight + 1
Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
wait(waitTime)
end
Gate Script
local GreenGate = game.Workspace.GreenGate
function ToggleGate()
local Time = game.Lighting.ClockTime
if Time == 6.5 then--Time that the gate will open
game.Workspace.GreenGate.Parent = workspace
print("Gate Opening")
GreenGate.CanCollide = false--Allows players to walk through
GreenGate.Transparency = 1--Makes it so players cant see it
elseif Time == 12 then--Time that gate closes
workspace.GreenGate.Parent = game.Workspace
print("Gate Closing")
GreenGate.CanCollide = true--Makes it so players cant walk through
GreenGate.Transparency = 0--Players can see the part
end
end
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(ToggleGate)
local Time = game.Lighting
A if statement will check if the condition is equal to something once.
I recommend using “.Changed” to and see if it is the certain time.
Rough Template Example:
local Lighting= game.Lighting
Lighting.ClockTime.Changed:Connect(function()
if (IF Time between 6.5 and 12) and GateIsNotOpened then
OpenGate()
else if (Time is between 12 and 6.5) and Gate is Opened then
CloseGate()
end
end)
Where would I put this? I put it here but it is showing errors in the script.
local GreenGate = game.Workspace.GreenGate
function ToggleGate()
local Lighting= game.Lighting.ClockTime
Lighting.ClockTime.Changed:Connect(function()
if ( time between 6.5 and 12) and GateIsNotOpened then
OpenGate()
else if (Time is between 12 and 6.5) and Game is Opened then
CloseGate()
end
end)
end
end
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(ToggleGate)
local Time = game.Lighting
This is a “Rough Draft Format” as what would and could work.
In Lua,
( Time between 6.5 and 12)--This Doesnt Exist
if Time >= 6.5 and Time <= 12 then -- This would be the Propper format,
and GateIsNotOpened then-- The game doesn't know if the gate is opened or not
--you need to have some indicator like a true / false (bool) value to tell the difference
CloseGate() & OpenGate(), -- The game doent not know that these mean
--you need to define these functions and that you want the game to do when these functions are called.
--Ex.
local function PrintHi()
print("Hi")
end
PrintHi()--Will call the function PrintHi which will print Hi!
Your gate works but when I have my Day/Night script it is not changing to night time. When I change it with HdAdmin it changes to the time I set it to for a second then changes back to the start time.
local Lighting = game:GetService("Lighting")
local ServerStorage = game:GetService("ServerStorage")
local OPEN_TIME = 7 -- Open the gate past this time
local CLOSE_TIME = 19 -- Close the gate at this time
function ToggleGate()
local Time = Lighting.ClockTime
if Time >= OPEN_TIME and Time < CLOSE_TIME then -- Check if the current time is between OPEN_TIME and CLOSE_TIME
print("Gate open")
local Gate = workspace:FindFirstChild'Gate'
if Gate then
Gate.Parent = ServerStorage -- Put gate back in ServerStorage because gate should be open
end
elseif Time < OPEN_TIME or Time >= CLOSE_TIME then
print("Gate closed")
local Gate = ServerStorage:FindFirstChild'Gate'
if Gate then
Gate.Parent = workspace -- Put gate back into the game because gate should be closed
end
end
end
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(ToggleGate)
local TIME_SPEED = 60
local START_TIME = 9
local minutesAfterMidnight = START_TIME * 60
local waitTime = 60 / TIME_SPEED
while true do
local Delta = wait(0.1)
minutesAfterMidnight = minutesAfterMidnight + (Delta * TIME_SPEED)
Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
end