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)
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)
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)
Tysm, this worked very well! @PolyLacticSugarcane @RestoredValour That works too, but I moved the light = false back one space for it to function correctly