Code is looking at ClassName rather than its Name errors out every time

Basically what’s supposed to happen is when the games TimeOfDay reaches a certain point the Street Lights are supposed to turn on/off. Ex TimeOfDay = 18 then lights on. TimeOfDay = 06 then Lights off.
However at the moment its just errors out when it hits the on state. Throws error “Lights is not a valid member of MeshPart” Not exactly sure why its looking at the Class Name rather than the given name.



image

  1. local lighting = game.Lighting

  2. local newTime = game.Lighting.ClockTime;

  3. function getThing()

  4. for i,v in pairs(game.Workspace.StreetLights[“Street Lights”]:GetChildren()) do

  5.  v.Lights.SpotLight.Brightness = 1
    
  6.  v.Lights.SpotLight.Brightness = 0
    
  7.  v.Lights.SpotLight.Brightness = 1
    
  8.  v.Lights.SpotLight.Brightness = 2
    
  9. end
    
  10. for i,v2 in pairs(game.Workspace.StreetLights["Street Lights_Small"]:GetChildren()) do
    
  11. 	v2.Light.PointLight.Brightness = .5
    
  12. 	v2.Light.PointLight.Brightness = 0
    
  13. 	v2.Light.PointLight.Brightness = .5
    
  14. 	v2.Light.PointLight.Brightness = 1
    
  15. end
    
  16. for i,v3 in pairs(workspace.StreetLights:GetChildren()) do
    
  17. 	if v3:FindFirstChild'Neon' then
    
  18. 		for _,v4 in pairs(v3.Neon:GetChildren()) do
    
  19. 			v4.Material = "Neon"
    
  20. 		end
    
  21. 	end
    
  22. end
    
  23. end

  24. function thing2()

  25. for i,v in pairs(game.Workspace.StreetLights["Street Lights"]:GetChildren()) do 
    
  26. 	v.Lights.SpotLight.Brightness = 0
    
  27. end
    
  28. for i,v2 in pairs(game.Workspace.StreetLights["Street Lights_Small"]:GetChildren()) do
    
  29. 	v2.Light.PointLight.Brightness = 0
    
  30. end
    
  31. for i,v3 in pairs(workspace.StreetLights:GetChildren()) do
    
  32. 	if v3:FindFirstChild'SmoothPlastic' then
    
  33. 		for _,v4 in pairs(v3.Neon:GetChildren()) do
    
  34. 			v4.Material = "SmoothPlastic"
    
  35. 		end
    
  36. 	end
    
  37. end
    
  38. end

  39. while wait(1) do

  40. local Time= lighting:GetMinutesAfterMidnight();
    
  41. lighting:SetMinutesAfterMidnight( Time + 1)
    
  42. local NewTime = game.Lighting.TimeOfDay
    
  43. if Time == 6*60 then
    
  44. 	thing2()
    
  45. elseif Time == 18*60 then
    
  46. 	getThing()
    
  47. end
    
  48. game.ReplicatedStorage.Events.MainClientUpdater:FireAllClients("Time", NewTime)
    
  49. end

Ok, so, with your first for loop(and it appears all your other loops), you are getting the children of the Street Lights model. And when you say v.Light, this is trying to look for a part that is a descendant of the Street Lights model. ‘v’ in this case, is referring to the children in the model. It is not referring to the classname, it just cant find a part in the Street Lights model that has a child named Light. You could do if v.Name == "Light" then and then v would equal “Light”.

Also, I’m not sure if you mean to iterate through every model named Street Light, but with how you set it up, it is only going to go through one model.

The for loop in your getThing() function iterates through all children in the street light, starting with the frame. It’s looking for a SpotLight object inside Frame.

local lighting = game.Lighting
local newTime = game.Lighting.ClockTime;
function getThing()
    for i,v in pairs(game.Workspace.StreetLights[“Street Lights”]:GetChildren()) do
         v.Lights.SpotLight.Brightness = 1
         v.Lights.SpotLight.Brightness = 0
         v.Lights.SpotLight.Brightness = 1
         v.Lights.SpotLight.Brightness = 2
    end



    for i,v2 in pairs(game.Workspace.StreetLights["Street Lights_Small"]:GetChildren()) do
	    v2.Light.PointLight.Brightness = .5
	    v2.Light.PointLight.Brightness = 0
	    v2.Light.PointLight.Brightness = .5
	    v2.Light.PointLight.Brightness = 1
     end



     for i,v3 in pairs(workspace.StreetLights:GetChildren()) do
	      if v3:FindFirstChild'Neon' then
		      for _,v4 in pairs(v3.Neon:GetChildren()) do
			     v4.Material = "Neon"
		      end
	      end
      end
end


function thing2()
    for i,v in pairs(game.Workspace.StreetLights["Street Lights"]:GetChildren()) do 
	     v.Lights.SpotLight.Brightness = 0
    end



    for i,v2 in pairs(game.Workspace.StreetLights["Street Lights_Small"]:GetChildren()) do
	     v2.Light.PointLight.Brightness = 0
    end


    for i,v3 in pairs(workspace.StreetLights:GetChildren()) do
	     if v3:FindFirstChild'SmoothPlastic' then
		     for _,v4 in pairs(v3.Neon:GetChildren()) do
			     v4.Material = "SmoothPlastic"
		     end
	    end
    end
end


while wait(1) do
    local Time= lighting:GetMinutesAfterMidnight();
    lighting:SetMinutesAfterMidnight( Time + 1)
    local NewTime = game.Lighting.TimeOfDay

    if Time == 6*60 then

	    thing2()

    elseif Time == 18*60 then

	    getThing()

    end

    game.ReplicatedStorage.Events.MainClientUpdater:FireAllClients("Time", NewTime)

end

Please can use preformatted text (Ctrl + Shift + C)

First, please do a namecheck and child check for all of X instances.

Don’t do StreetLights[“Street Lights”] because there are like 10 of them.

Instead do if v.Name == “Street Lights” then…

Next, use print to debug your code.

Lastly, fix any errors if you don’t see a string you wanted printed.

Yeah I’m wanting it to look through every model named “Street Light” so I don’t have a line for every light I want on. What your suggesting I do is something this?
for i,v in pairs(game.Workspace.StreetLights:GetChildren()) do
if v.Name == "Street Lights then
Blah

If you want it to go through everything, then yes, you would have to go about it by doing it that way. Except my answer changes a little from my original. Instead of doing if v.Name == "Light" then, you would do v.Lights.SpotLight.Brightness = 1 after checking if v.Name == “Street Lights”.

1 Like