Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make it so that when a player passes through an invisable block all lights turn off?

I made a folder called LightingSystem,inside it a folder called Lights,inside the folder lights I added parts named Light and in them SurfaceLights. Inside the LightingSystem folder is a TouchPart and a script that makes the whole thing work. It doesn't work.

local LightingSystemFolder = script.Parent

local Lights = LightingSystemFolder:WaitForChild("lights")

local TouchPart = LightingSystemFolder:WaitForChild("TouchPart")

TouchPart.Touched:Connect(function(touch)
	
	if not humanoid then
		return
		
		for i, Light in pairs(Lights:GetChihldren())
		do
			local lightBulb = Light:WaitForChild("SurfaceLight")
			lightBulb.Enabled = false
			
			print("hello light")
		end
	end
end)

Help please

1 answer

Hey there! You can actually achieve this by using a Touch event like you already have.

What you did was almost correct, here are the errors:

local LightingSystemFolder = script.Parent
local Lights = LightingSystemFolder:WaitForChild("lights")
local TouchPart = LightingSystemFolder:WaitForChild("TouchPart")

TouchPart.Touched:Connect(function(touch)
if not humanoid then

		--Right here you used "humanoid" but you did not make a variable for it!		

	    return
	 
	    for i, Light in pairs(Lights:GetChihldren())

		--Spelling error in "Chihldren" you dont want that!

	    do
	        local lightBulb = Light:WaitForChild("SurfaceLight")
	        lightBulb.Enabled = false
	 
	        print("hello light")
	    end
	end
end)

to fix this you can do this

local LightingSystemFolder = script.Parent
local Lights = LightingSystemFolder:WaitForChild("lights")
local TouchPart = LightingSystemFolder:WaitForChild("TouchPart")

TouchPart.Touched:Connect(function(touch)
local humanoid = touch.Parent.Humanoid

if humanoid == nil then return end
	 
	    for i, Light in pairs(Lights:GetChildren())
	    do
	        local lightBulb = Light:WaitForChild("SurfaceLight")
	        lightBulb.Enabled = false
	 
	        print("hello light")
	    end
	end
end)

By the way, this script must be directly parented to the part you want to use to trigger the light bulbs. Placing this script in a folder in LightingSystem would most definitely not work.

If this worked do consider accepting it! If not comment below what error it gives you

This discussion is preserved as it was. New answers, comments, and votes are closed.