Clock time detection not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m trying to make sure that all of my game’s street lights turn on if the game starts at a certain time or stay off.

  1. What is the issue? Include screenshots / videos if possible!

I wrote this code here:

local descendants = game.Workspace.Model:GetDescendants()



for index, descendant in pairs(descendants) do
	if descendant.Name == "StreetLightBulbI" then
		if descendant:IsA("MeshPart")then
		descendant.Transparency = 1
		print("hi")
		end
	end
end


local Light = game.ReplicatedStorage.StreetLight

-- SET FOR WHEN FIRST MADE
if game.Lighting.ClockTime >= 6.5 and game.Lighting.ClockTime <= 17.7 then
	-- off
	for index, descendant in pairs(descendants) do
		if descendant.Name == "StreetLightBulbI" then
			if descendant:IsA("MeshPart")then
				descendant.Transparency = 0
				descendant.BrickColor = BrickColor.new("Institutional white")
				descendant.Material = "ForceField"
				
				print("HAAAA")
			end
		end
	end
elseif  game.Lighting.ClockTime <= 6.5 and game.Lighting.ClockTime >= 17.7 then
	-- on
	for index, descendant in pairs(descendants) do
		if descendant.Name == "StreetLightBulbI" then
			if descendant:IsA("MeshPart")then
				descendant.Transparency = 0
				descendant.BrickColor = BrickColor.new("Institutional white")
				descendant.Material = "Neon"
				local steetLightClone = Light:Clone()
				steetLightClone.Parent = descendant
				print("AHHHH")
			end
		end
	end
else
	print("HUH")
end

Every time I play my game it prints “HUH”, and I’m not sure why. This is probably just a simple error but I cannot figure it out for the life of me.
Thanks in advance!

1 Like

Your second elseif should be an or instead of an and. The time can’t be less than 6.5 and greater than 17.7 at the same time.

Change:

elseif  game.Lighting.ClockTime <= 6.5 and game.Lighting.ClockTime >= 17.7 then

To

elseif  game.Lighting.ClockTime <= 6.5 or game.Lighting.ClockTime >= 17.7 then

Additionally, this check will only execute once so if you plan on changing the time dynamically, you will need to put this check in something like a while loop or connect it to RunService.Stepped.

2 Likes

Thank you! I changed the code to include the or statements instead of the and, but now it does not print anything :confused:

1 Like

What does your workspace’s hierarchy look like? I’ve set up a repro that appears to work fine, so I’m curious as to how your workspace is laid out.

The code provided won’t work unless you name the mesh parts StreetLightBulbI, and put them in workspace.Model. Also, StreetLight must be in ReplicatedStorage.

2 Likes

This is how it looks


And I have confirmed that those are mesh parts. I used “get descendants” in an attempt to not have to scan for the street lights themselves.

1 Like

Try adding prints and see if anything outputs. This will allow you to check to see if it even loops over the descendants, and if it sets stuff.

1 Like

ok So I added the prints and it really helped a lot, but now I’m confused to why the script isn’t detecting the names of the bulbs.

100% the names are the same, but the print that’s highlighted is the last one that prints.

1 Like

What is the result of adding something like

print(descendant.Name)

Maybe the strings are slightly different, try copying the result and checking for that instead.

2 Likes

Actually I did add that and it doesn’t even print the name of the descendant.

disreguard

The name actually prints as weld

Edit 3: I found the problem

The come is referncing a random model in workspace :man_facepalming:

Thanks for your help!

1 Like

Do you have multiple Models in Workspace named Model? Make sure your items are in a Model named Model in Workspace, as that’s where your script is looking for descendants.

Glad that was the issue. Might be a good idea in the future to give everything unique names, instead of just Model.

2 Likes

Annnnd the issue wasn’t really fixed. I just tried running the code instead scanning workspace and I still don’t get past that print

Edit: got a script timeout maybe from the prints, I’m going to remove it for now to see if that fixes it.

1 Like

In the second statement it says if time is less then 6.5 and greater then 17.7 which is the problem. You mean less then 6.5 or greater then 17.7.

1 Like
if  game.Lighting.ClockTime >= 6.5 or game.Lighting.ClockTime <= 17.7 then
	--print("1124")
	-- off
	for index, descendant in pairs(descendants) do
		--print("1124")
		--print(descendant.Name)
		if descendant.Name == "StreetLightBulbI" then
			print("1124")
			if descendant:IsA("MeshPart")then
				print("1124")
				descendant.Transparency = 0
				descendant.BrickColor = BrickColor.new("Institutional white")
				descendant.Material = "ForceField"
				
				print("HAAAA")
			end
		end
	end
elseif  game.Lighting.ClockTime <= 6.5 or game.Lighting.ClockTime >= 17.7 then
	print("1124")
	-- on
	for index, descendant in pairs(descendants) do
		--print("1124")
		if descendant.Name == "StreetLightBulbI" then
		--	print("1124")
			if descendant:IsA("MeshPart")then
				print("1124")
				descendant.Transparency = 0
				descendant.BrickColor = BrickColor.new("Institutional white")
				descendant.Material = "Neon"
				local steetLightClone = Light:Clone()
				steetLightClone.Parent = descendant
				print("AHHHH")
			end
		end
	end
else
	print("HUH")
end

These are the updated statements, but even if the time is at 18, the lights remain off. I’m not sure if I’ve got this down or not but it’s quite confusing to say the least.

You changed the first if statment to or. The first if statement needs to change back to how it was before with a and instead of a or and the second if statement is good.

2 Likes

Thank you! Thanks to you my city can now have working lights!

Ignore the fact that it’s daytime :joy: snipping tool crashed :unamused: