Generator script won't work

Hello!
I need help with my script, I don’t know what’s wrong with it.
The script is supposed to make light on the generator green/red, and make all pointlights in lightemitter off/on, additionally when I click the button, it’s supposed to turn back green and lights on, after 20 seconds the light should become red and all the pointlights in lightemitters would turn off.

local light = game.Workspace.lightez
local button = game.Workspace.buttonez.ClickDetector
local PointLights = game.Workspace.LightEmitter.PointLight
function Green()
light.BrickColor = BrickColor.new(“Lime green”)
PointLights.Enabled = true
end
function Red()
light.BrickColor = BrickColor.new(“Really red”)
PointLights.Enabled = false
end
function Fix()
if button.MouseClick:Connect() then
Gen()
end
end
function Gen()
Green()
wait(20)
Red()
end
Gen()

Here’s the script, it’s put into workspace.
I have no idea how to fix it, I tried many ways.

Some advice?

1 Like

I’ve fixed the code for you and made it beautify it for you. You added a function which was not identified in the function you put in

function Fix()
if button.MouseClick:Connect() then
Gen() -- Error
end

Fixed Code:

local light = game.Workspace.lightez
local button = game.Workspace.buttonez.ClickDetector
local PointLights = game.Workspace.LightEmitter.PointLight

function Green()
  light.BrickColor = BrickColor.new(“Lime green”)
  PointLights.Enabled = true
end

function Red()
  light.BrickColor = BrickColor.new(“Really red”)
  PointLights.Enabled = false
end

function Gen()
  Green()
  wait(20)
  Red()
end

function Fix()
  if button.MouseClick:Connect() then
  Gen()
end

Gen()

It still doesn’t work, the light which was supposed to turn off doesn’t turn off, and the light which was supposed to be green/red is still with default part color (white).
https://gyazo.com/a1bc5fad77bff60c2529a2982aa06ef5

Are you able to check and see if your functions are running when you click it? I wouldn’t do .MouseClick for bricks. I would try and use a ClickDetector for that sort of things as well. I’ll try and test it in my own game, and then I will shoot you over the code.

I apologize, you did use a clickdetector. One sec then

https://gyazo.com/456b16d2c19cb90064bb130203308def
This is how my function works… here is the script for it.

local button = script.Parent
local light = game.Workspace.lightz
local pointlight = light.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

end)

Adjust the variables and the locations of your bricks as you please. If I missed anything, let me know.

2 Likes

Thank you for help, however, it was supposed to turn off itself! I’ll work on it by myself, but, huge thanks for help.

To have it turn off itself, just use a wait feature in the same script. You can use math.random() if you want it to occur at random times.