Making a Door that closes between certain ClockTime values

  1. **What do you want to achieve?
    I am trying to make a door that opens at 5:30 AM (in-game time) and closes at 8:30 PM.

  2. What is the issue? Include screenshots / videos if possible!
    I have tried the code below, but it doesn’t work. when the ClockTime reaches 5.5, the ‘CanCollide’ Property of the door remains true.

  3. What solutions have you tried so far?
    I tried looking here and tried multiple scripts, but nothing has worked. :pensive:

Screenshot 2024-10-07 152930
The script is meant to open the door (union part named ‘Door’)

(set the ‘CanCollide’ property to false and the ‘Transparency’ property to 1)

whenever the time reaches 5:30 AM, and then close the door

(set the ‘CanCollide’ property to true and the ‘Transparency’ property to 0)

when it reaches 8:30 PM

The comments in the script are just me explaining my thought process when I wrote the script

local Door = script.Parent
local ClockTime = game.Lighting.ClockTime

while true do
	if ClockTime >= 17.5 or ClockTime <= 5.5 then
        -- checking if the time is between 8:30 PM and 5:30 AM
        -- (when the door should be closed)
		Door.CanCollide = true
                Door.Transparency = 0
	else
        --if it is not between 8:30 PM and 5:30 AM, open the door
		Door.CanCollide = false
		Door.Transparency = 1
	end
	wait(0.1)
end

I am very new to scripting
Any help will be appreciated

3 Likes

Put clock time inside the loop. Not in the global scope [top of script]

2 Likes

Smth like this should work:

local Door = script.Parent

while task.wait() do
	local ClockTime = game.Lighting.ClockTime

	if ClockTime >= 17.5 or ClockTime <= 5.5 then
		Door.CanCollide = true
		Door.Transparency = 0
	else
		Door.CanCollide = false
		Door.Transparency = 1
	end
end

The issue is that you’re saving time when the game loads so what you’re doing is checking an unchanging number over and over again. When the ClockTime variable is within the loop, it checks for the actual current clock time.

2 Likes

Ohhh, that makes sense, thank you!

I just tried it and it works - thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.