Error: Workspace.Map.LampPost.Script:53: attempt to index a nil value
Automatic open and close LamPost, WHY "attempt to index a nil value"??
local oh,om = 6,30 -- Open Time (hours,minutes) local ch,cm = 18,00 -- Close Time (hours, minutes) local l = game:service("Lighting") if (om == nil) then om = 0 end if (cm == nil) then cm = 0 end function TimeChanged(lf,lr) local ot = (oh + (om/60)) * 60 local ct = (ch + (cm/60)) * 60 if (ot < ct) then if (l:GetMinutesAfterMidnight() >= ot) and (l:GetMinutesAfterMidnight() <= ct) then lf.Enabled = false lf.Parent.BrickColor = BrickColor.Black() lf.Parent.Material = Enum.Material.SmoothPlastic ------------------------------------------------- lr.Enabled = false lr.Parent.BrickColor = BrickColor.Black() lr.Parent.Material = Enum.Material.SmoothPlastic else lf.Enabled = true lf.Parent.BrickColor = BrickColor.White() lf.Parent.Material = "Neon" ------------------------------------------------- lr.Enabled = true lr.Parent.BrickColor = BrickColor.White() lr.Parent.Material = "Neon" end elseif (ot > ct) then if (l:GetMinutesAfterMidnight() >= ot) or (l:GetMinutesAfterMidnight() <= ct) then lf.Enabled = false lf.Parent.BrickColor = BrickColor.Black() lf.Parent.Material = Enum.Material.SmoothPlastic ------------------------------------------------- lr.Enabled = false lr.Parent.BrickColor = BrickColor.Black() lr.Parent.Material = Enum.Material.SmoothPlastic else lf.Enabled = true lf.Parent.BrickColor = BrickColor.White() lf.Parent.Material = "Neon" ------------------------------------------------- lr.Enabled = true lr.Parent.BrickColor = BrickColor.White() lr.Parent.Material = "Neon" end else print("TimeChange 'LampPostScript' error. ") end end for i,v in pairs(script.Parent:GetChildren()) do local lf = v:FindFirstChild("Left").Light.SurfaceLight local lr = v:FindFirstChild("Right").Light.SurfaceLight TimeChanged(lf,lr) game.Lighting.Changed:connect(function(property) if (property == "TimeOfDay") then TimeChanged(lf,lr) end end) end
Hi!
When you are getting the children of the parent, it will also try to find "Left" and "Right" inside of the script you're using, which will return in a nil value. You need to make sure it is not indexing the script.
for i,v in pairs(script.Parent:GetChildren()) do if v:IsA("Model") then --Checks if v is a model. local lf = v:FindFirstChild("Left").Light.SurfaceLight local lr = v:FindFirstChild("Right").Light.SurfaceLight TimeChanged(lf,lr) game.Lighting.Changed:connect(function(property) if (property == "TimeOfDay") then TimeChanged(lf,lr) end end) end
Click "Accept Answer" if this was what you were looking for! Comment if you have any more concerns!