Automatic turn off won't work

Hello!
I need help with the script, it turns off/on when I click the button, however, it doesn’t turn off itself.

local button = script.Parent
local light = game.Workspace.genez.lightez
local pointlight = game.Workspace.Lightemitter.PointLight
local ClickDetector = button.ClickDetector
local on = false

ClickDetector.MouseClick:Connect(function()
if on == false then
on = true
pointlight.Enabled = true
light.BrickColor = BrickColor.new(“Lime green”)
else
on = false
pointlight.Enabled = false
light.BrickColor = BrickColor.new(“Really red”)
end

if on == true then
	pointlight.Enabled = true
	light.BrickColor = BrickColor.new("Lime green")
elseif on == false then
	pointlight.Enabled = false
	light.BrickColor = BrickColor.new("Really red")
end

if on == true then
	wait(10)
	on = false
end

end)

How can I make it work?

You are only setting the variable to false. You have to disable the PointLight and change the BrickColor too.

How can I commit this change though?

With the way you’ve done it, a change like this is simply just,

if on == true then
	wait(10)
	on = false
	pointlight.Enabled = false
	light.BrickColor = BrickColor.new(“Really red”)
end

Which is how I believe you intended on making this if I’m not mistaken

So, which part of the script should I replace?

I’ll rewrite the script to so where needs to be, with a few minor changes here and there how I would do and explain why I did these

local button = script.Parent
local light = game.Workspace.genez.lightez
local pointlight = game.Workspace.Lightemitter.PointLight
local ClickDetector = button.ClickDetector
local on = false

ClickDetector.MouseClick:Connect(function()
	if on then
		pointlight.Enabled = true
		light.BrickColor = BrickColor.new("Lime green")
		wait(10)
		on = false
		pointlight.Enabled = false
		light.BrickColor = BrickColor.new("Really red")
	else
		pointlight.Enabled = false
		light.BrickColor = BrickColor.new("Really red")
	end
	on = not on
end)

First thing you probably notice is how there’s no == true and == false. In Roblox there other ways of showing that, if you just write the variable name, that will make it check if the condition is true, I also made the elseif into an else since if on is not true, then it obviously has to be false.

I also combined the condition to automatically turn the light of into the if statement because that’s where it should be in the first place. I’m not saying how you did it was wrong, I’m just saying other ways you could put code around to see what looks cleaner and all

The last thing I did was instead of manually inverting the boolean variable I used,

on = not on

Which inverts for us instead (if on is true then it becomes false and vice versa).

Hopefully this helped you understand a bit more of other ways of doing stuff in terms of scripting and I hope this also helped in terms of the solution

2 Likes

Thank you for help! It worked.

1 Like

Glad that I helped you out! If you ever have any more issues, don’t be afraid to make another post in Scripting Support!

1 Like