Attempt to index nil with "Part"

Hello, can please someone help me ??. I’m trying to turn on a “SurfaceLight” when the Time goes after 18. But it says “Workspace.GameWorld.Streetdeko.Lamps.Script:12: attempt to index nil with ‘Part’”.
Script ‘Workspace.GameWorld.Streetdeko.Lamps.Script’, Line 12.

Explorer:
Help

Script:

while true do
	wait(2.5)
	for i,v in pairs(script.Parent.Parent.Lamps.Lantern:GetChildren()) do
	wait(2.5)
	local light = v:FindFirstChild("SurfaceLight")
	if light then 
		if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
				light.Part.SurfaceLight.Enabled = false 
			print("light")
		end
		else if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then
				light.Part.SurfaceLight.Enabled = true 
			print("light")
		end
	end
	end
end

Hope someone can help :slight_smile: !

You put an end after your inner if, so that elseif is actually part of the top if. Also, lua uses elseif as one word.

Basically your code is like:

if light then
  if ... then
    ...
  end
else
  if ... then
    ...
  end
end

When it seems like you want

if light then
  if ... then
    ...
  elseif ... then
    ...
  end
end
1 Like

Also instead of saying light.Part, try with light:FindFirstChild(“Part”)

1 Like

Hm Okey. But now it says i forgot to close the line “Error: (10,38) Syntax error: Expected ‘end’ (to close ‘do’ at line 3), got ; did you forget to close ‘then’ at line 9?”

like this?

while true do
	wait(2.5)
	for i,v in pairs(script.Parent.Parent.Lamps.Lantern:GetChildren()) do
	wait(2.5)
	local light = v:FindFirstChild("SurfaceLight")
	if light then 
		if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
				light:FindFirstChild("Part").SurfaceLight.Enabled = false 
		elseif game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then
				light:FindFirstChild("Part").SurfaceLight.Enabled = true 
	     end
	 end
  end
end

Because now then its over 18 nothing happends.

I’m assuming this error is because you have light already as the surface light and are trying to find a light and a surface light under the surface light. Instead of

light.Part.SurfaceLight.Enabled = true

do

light.Enabled = true

I tried it now but the Error is now:

Workspace.GameWorld.Streetdeko.Lamps.Script:12: attempt to index nil with 'Enabled'
Script 'Workspace.GameWorld.Streetdeko.Lamps.Script', Line 12  -  Studio - Script:12

Script:

while true do
	wait(2.5)
	for i,v in pairs(script.Parent.Parent.Lamps.Lantern:GetChildren()) do
		wait(2.5)
		local light = v:FindFirstChild("SurfaceLight")
		if light then 
			if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
				light.Enabled = true
				print("light")
			end
		else if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then
				light.Enabled = true
				print("light")
			end
		end
	end
end

add a print(light) and check what’s this

this works for me though?
this is the script I used:

while true do
	wait(2.5)
	for i,v in pairs(script.Parent.Parent.Lamps.Lantern:GetChildren()) do
		wait(2.5)
		local light = v:FindFirstChild("SurfaceLight")
		if light then 
			if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then
				light.Enabled = false 
				print("light")
			end
		else if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then
				light.Enabled = true 
				print("light")
			end
		end
	end
end

I have done that and it says: “nil”

Oh, okay. That’s Weird. Because when i use that Script it says: Workspace.GameWorld.Streetdeko.Lamps.Script:12: attempt to index nil with ‘Enabled’

is there something that deletes the surface lights then? I’m not sure what else the problem could be

then try to print the v this time and see what it is

also check if the parts are CanCollide true and Anchored true

Its just this Script so it can’t be deleted

check if the parts are CanCollide true and Anchored true

They are CanCollide true and Anchored true

and im testing the print(v) now

The print is “Part”. That’s what is shown in the output

So far, you have one thing: the parent of v is Part which is good. Now, when testing on studio, check with explorer if the SurfaceLight are still here

So i played now and i see that they are still there but not enabled. (because i wanna do that lol) :slight_smile: