You can write your topic however you want, but you need to answer these questions:
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.
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!
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.
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.
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.
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.