Light Button Script Help

I’m a beginner scripter, I am trying to script a light button that turns on a part named “Light1” which is the light that shines a SurfaceLight for 3 seconds when pressed. There is something wrong with my code and I spent like 40 minutes trying to figure it out. Please help.

db = false
door = false
light = false

workspace.LightButton.ClickDetector.MouseClick:Connect(function()
	if not db then db = true
		if not light then light = true
			workspace.Light1 = true
			script.Parent.LightButton.BrickColor = BrickColor.new("Light blue")
			db = false
			wait(3)
		elseif light then light = false
			workspace.Light1 = false
			script.Parent.LightButton.BrickColor = BrickColor.new("Dark stone grey")
			db = false
		end
	end
end)

image
image

You are setting an instance to true, not its property. Change workspace.Light1 to this:
workspace.Light1.Enabled = true

Also, use task.wait(3) instead of wait().

Edit: Im guessing your light is inside of a part, so do this instead if it is
workspace.Light1.SurfaceLight(or whatever the name is).Enabled = true

2 Likes

The output window is telling you that Light1 is not in the Workspace.

Maybe that is not where Light1 is, or maybe it is not named Light1.

And Enabled is a property of a light, not true (as PolyLacticSugarcane pointed out.)

Could you please provide the screenshot of the light’s path? It’ll help solve the problem much faster.

image

Show the entire path, like the entire explorer basically.

@PolyLacticSugarcane @mc7oof Yes, thank you both! That helped a lot.

1 Like

I messed around with the code, and i got it to work, but i want it so when the player presses again, it will work. I cant figure it out

door = false
light = false

workspace.LightScript.Light1.SurfaceLight.Enabled = false
workspace.LightScript.LightButton.ClickDetector.MouseClick:Connect(function()
	if not db then db = true
		if not light then light = true
			workspace.LightScript.Light1.SurfaceLight.Enabled = true
			workspace.LightScript.LightButton.BrickColor = BrickColor.new("Light blue")
			db = true
			task.wait(2)
			workspace.LightScript.Light1.SurfaceLight.Enabled = false
			workspace.LightScript.LightButton.BrickColor = BrickColor.new("Dark stone grey")
			db = false
			task.wait(1)
			workspace.LightScript.LightButton.BrickColor = BrickColor.new("Light blue")
			workspace.LightScript.Light1.SurfaceLight.Enabled = false
		end
	end
end)

Seems like you are never setting “light” back to false after running if not light then light = true?

Therefore, you’d have to change to:

door = false
light = false
db = false

workspace.LightScript.Light1.SurfaceLight.Enabled = false
workspace.LightScript.LightButton.ClickDetector.MouseClick:Connect(function()
	if not db then db = true
		if not light then light = true
			workspace.LightScript.Light1.SurfaceLight.Enabled = true
			workspace.LightScript.LightButton.BrickColor = BrickColor.new("Light blue")
			db = true
			task.wait(2)
			workspace.LightScript.Light1.SurfaceLight.Enabled = false
			workspace.LightScript.LightButton.BrickColor = BrickColor.new("Dark stone grey")
			db = false
			task.wait(1)
			workspace.LightScript.LightButton.BrickColor = BrickColor.new("Light blue")
			workspace.LightScript.Light1.SurfaceLight.Enabled = false
			light = false
		end
	end
end)

Try this

db = false
door = false
local LightObject = game.Workspace.LightScript.Light1.SurfaceLight
local LightButton = game.Workspace.LightScript.LightButton

LightObject.Enabled = false
workspace.LightScript.LightButton.ClickDetector.MouseClick:Connect(function()
	if not db then
 		db = true
		workspace.LightScript.LightButton.ClickDetector.MaxActivationDistance = 0 --this isnt required but it looks better in game
		LightObject.Enabled = true
		LightButton.BrickColor = BrickColor.new("Light blue")
		task.wait(2)
		LightObject.Enabled = false
		LightButton.BrickColor = BrickColor.new("Dark stone grey")
		task.wait(1)
		--you can also move the LightObject.Enabled = true line down here if you want it to be 3 seconds
		LightButton.BrickColor = BrickColor.new("Light blue")
		LightButton.ClickDetector.MaxActivationDistance = 32
		db = false
	end
end)
1 Like

Tysm, this worked very well! @PolyLacticSugarcane
@RestoredValour That works too, but I moved the light = false back one space for it to function correctly

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.