Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make it where when a "TimeOfDay" is a certain time it will make a part CanCollide?

if game.Lighting.TimeOfDay >= '09:30:00' then
	game.Workspace.BankSign.SignPart.TextLabel.TextLabel.Text = 'Bank is Currently Opened!'
	game.Workspace.BankMainDoor.CanCollide = true
	
elseif
	game.Lighting.TimeOfDay >= '09:30:00' then
	game.Workspace.BankSign.SignPart.TextLabel.TextLabel.Text = 'Bank is Currently Closed!'
	game.Workspace.BankMainDoor.CanCollide = false
	
end

2 answers

✓ Accepted answer

The problem here is that the TimeOfDay property is a string. Meaning that you're trying to check if a string is greater than another string. What you want to do can be achieved using the GetPropertyChangedSignal method of Instances and the ClockTime property of Lighting. I've changed the TimeOfDay 09:30:00 to its ClockTime equivalent, 9.5

game:GetService("Lighting"):GetPropertyChangedSignal("ClockTime"):Connect(function()
	local CT = game:GetService("Lighting").ClockTime
	
	if CT >= 9.5 and CT <= 15.5 then -- 9.5 is 9:30 and 15.5 is 3:30
		game.Workspace.BankSign.SignPart.TextLabel.TextLabel.Text = 'Bank is Currently Opened!'
		game.Workspace.BankMainDoor.CanCollide = true
	else
		game.Workspace.BankSign.SignPart.TextLabel.TextLabel.Text = 'Bank is Currently Closed!'
		game.Workspace.BankMainDoor.CanCollide = false
	end
end)

I hope my answer helped solve your problem! If it did please remember to mark it as correct!

2 comments

0

Okay so it look, will the time cycle so it can auto do Open and Close after certain that and before that time? So say if I want the bank to stay open from 9:30 (9.5) to 3:30PM? And it close after 3:30 PM up to 9:30 (9.5)

0

I've updated the code for what you want.

First of all, both your if and elseif statements have the exact same condition. Secondly, you can constantly check for the time in a while loop using Lighting:GetMinutesAfterMidnight().

while true do
	if game.Lighting:GetMinutesAfterMidnight() == ((9 * 60) + 30) then 
	-- (9 * 60) represents 9 hours, (+30) represents the additional 30 minutes
		-- it's 9:30 am, open doors
	elseif game.Lighting:GetMinutesAfterMidnight() == ((21 * 60) + 30) then
	-- same idea as above, 21:30 is 9:30 pm
		-- its 9:30 pm, close doors
	end

	wait(0.1)
end

In this script I'm only checking when the time is exactly 9:30, however if you feel its necessary to check if its between 9:30 am and 9:30 pm then you can check if the game.Lighting:GetMinutesAfterMidnight() >= ((9 * 60) + 30) and game.Lighting:GetMinutesAfterMidnight() < ((21 * 60) + 30)

EDIT:

You can also use Lighting.ClockTime rather than Lighting:GetMinutesAfterMidnight for a different representation of the time.

This discussion is preserved as it was. New answers, comments, and votes are closed.