Opening and closing a gate based on ClockTime

I don’t know how to close a post

2 Likes

Ohhh, okay. I’ll try that! Thanks for the suggestion.

1 Like

If there is not a loop in that code once it checks once it will never check again. Try this:

game:GetService("RunService").Heartbeat:Connect(function()
--Checking the time
end)

No, that’s wrong.
It’s because if you have a night/day script, its advancing in like 1 in its value or, 0.3 so it’s never getting to 6.4 or 18.8

Also, 6.4 and 18.8 are numbers, compared to a string, will never return true
And if you’re checking this once, it’s never going to work, unless you have time set to 18.8/6.4, but still will not work since you are comparing string.

1 Like

The day/night script I made doesn’t do it like that.

I do think @varjoy is right I got TimeOfDay mixed up with ClockTime however, try this: game.Lighting.ClockTime == 6.4 (remove quotation marks)

Yeah, but that still doesn’t work.

Try reading up on this: Lighting | Documentation - Roblox Creator Hub

Also are there any errors in the output?

Nope. No errors in the output, kinda weird not gonna lie.

The reason this is happening is that your code has no loop or signals to repeatedly check the value of game.Lighting.ClockTime, so your code runs only once at runtime (when the server starts) - since the ClockTime isn’t what you’re looking for when it runs, nothing happens.

You could use a loop to constantly check the time and act on it, as described above, however this isn’t great for performance because your loop could be unnecessarily running if the time isn’t changing for longer periods than the loop interval.

In my opinion the ideal solution is to perform your check every time the ClockTime property changes, like so:

local OPEN_TIME = 18.8 -- Open the gate past this time
local CLOSE_TIME = 6.4 -- 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)

Note that this is a very basic implementation based off of your code. If you’d like to improve this further, you should specify a range of Time to open/close the gate at, instead of doing it at specific numbers. This is because if you have a script incrementing ClockTime by 1, it will never reach 6.4, etc so your code won’t function.


Edited your title to only contain relevant information and outline the issue better.

Doesn’t work still, I can confirm that the day/night script I made goes up to 18.8 and obviously 6.4 .

It would be helpful if you could show us the day/night cycle script too, so we can see if the problem might be in there.

1 Like

Nevermind, I’m just gonna not use this and do something similar.