I am trying to make a light on and light off script but I can't remember how to make it so the script waits for the light to be created.
01 | lightOn = false |
02 | SmoothBlockModel = script.Parent |
03 |
04 | function onClicked() |
05 | if lightOn = = false then |
06 | local l = Instance.new( "PointLight" ) |
07 | l.Brightness = 4 |
08 | l.Range = 5 |
09 | l.Parent = script.Parent |
10 | lightOn = true |
11 | end |
12 | if lightOn = = true then |
13 | script.Parent.PointLight:Destroy() |
14 |
15 | end |
16 |
17 |
18 | end |
19 |
20 | script.Parent.ClickDetector.MouseClick:connect(onClicked) |
01 | lightOn = false |
02 | SmoothBlockModel = game.Workspace.SmoothBlockModel --for more effectiveness |
03 |
04 | function onClicked() |
05 | if lightOn = = false then |
06 | local l = Instance.new( "PointLight" ) |
07 | l.Brightness = 4 |
08 | l.Range = 5 |
09 | l.Parent = SmoothBlockModel --Use your variables |
10 | lightOn = true |
11 | if lightOn = = true then |
12 | if SmoothBlockModel:findFirstChild( "PointLight" ) then SmoothBlockModel.PointLight:Destroy() |
13 | end |
14 | end --Also leave all your ends together to make sure it runs all together. |
15 | end |
16 | end |
17 |
18 | SmoothBlockModel.ClickDetector.MouseClick:connect(onClicked) |
If this helped which I hope it did be sure to accept it.
I'll rewrite it for you. Don't declare variables and not use them. Legomaster38 is trying to enforce his own scripting rules on to you too - don't listen to him.
01 | local on = false |
02 | local block = script.Parent -- Make sure the script's parent actually is a block. |
03 |
04 | block.ClickDetector.MouseClick:connect( function () -- Try to minimize your functions. |
05 | if not on then -- "not" is a quick way of saying "if variable == false" |
06 | local pointLight = Instance.new( "PointLight" , block) -- We can set the parent in the same function |
07 | pointLight.Brightness = 4 |
08 | pointLight.Range = 5 |
09 | on = true |
10 | elseif on then -- Get rid of not and it means "if variable == true" |
11 | if block:FindFirstChild( "PointLight" ) then -- If a PointLight exists in the block |
12 | block.PointLight:Destroy() |
13 | end |
14 | end |
15 | end ) |