Opening and closing a gate based on ClockTime

Hello I am trying to make a gate to where it can open at a certain time in the game and close at a certain time in the game.

Day/Night Module

I have this script in ServerScriptService

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

I have this script in the part

local OPEN_TIME = 7 – Open the gate past this time
local CLOSE_TIME = 19 – Close the gate at this time

function ToggleGate()
local Time = game.Lighting.ClockTime

if Time == OPEN_TIME then
	game.ServerStorage.Gate.Parent = workspace
elseif Time == CLOSE_TIME then
	workspace.Gate.Parent = game.ServerStorage
end

end

game.Lighting:GetPropertyChangedSignal(“ClockTime”):Connect(ToggleGate)

5 Likes

Don’t you need to do something like

if time == 9 then
–open gate here
end

1 Like

Where would I put it? Where the OPEN_TIME and CLOSE_TIME is?

2 Likes

Are you doing it on like real time so you have real hours that acually is an hour in real life or do you use the time from roblox?

if it’s time from roblox it self if you have a new script you can do like

local Time = game.Lighting.ClockTime

if Time == 9 then
–Open the gate
end

if time == 6 then
–close the gate
end

2 Likes

Ok so if I just use the roblox time then the script in the part I can just delete everything else in the script and put this?

1 Like

Ye idk if it will work i recomend still saving your old script i don’t want to ruin everything for you i’m not a really profesional at scripting

but you can try to use for now instead of using the – open gate or --close gate
you can try using

print(“gate opening”)

if it say’s it in the output then it will work 100%

2 Likes

Ok… I am going to go try this now.

Ye be fast i gtg now i’m from belgium it’s 5:00 AM lol

1 Like

I put it in and I am not seeing anything in the output.

Do you se some errors or not if so tell me what the error is i will try to make a script like you so i can se it also

1 Like

When I set the time to 9 the onlything it says in the output is Gate Opening but its not opening

Oh that’s great now you only need to add a script that opens the gate and putt it in the part where i did say – gate open and then it will work how you wanted it. also after the == the 9 means on what time it’s going to open

1 Like

How would I set can collide off and transparancy to 1?

You can do that by selecting your gate by doing

local gate = script.parent – so the scripts needs to be in the gate it self

local Time = game.Lighting.ClockTime

if Time == 9 then
gate.CanCollide = false - you can walk trough it
gate.Transparecy = 1 – it will get transparent
end

if time == 6 then
gate.CanCollide = true - you can’t walk trough it
gate.Transparecy = 0 – it will not get transparent
end

But i recommend you using TweenService TweenService Tutorial cause it’s alot smoother and it will be more advanced.

1 Like

Oh ok… Thank you for your help.

1 Like

No problem it’s an easy thing to do this.
i going btw right now have a fun time creating your games!

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 -- 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
1 Like

Thank you for your help! I got it working.

1 Like