Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make my flashlight turn off if i press it the second time?

Asked by 5 years ago

I am trying to make a flashlight when you click it it turns on and when you click it again it turns off. My output keeps on saying "Expected 'end' (to close 'function' at line 5), got 'else'"

Here is my script, pay attention to the tool equipped part.

01local Tool = script.Parent
02 
03Tool.Equipped:Connect(function(Mouse)
04    print("Tool was equipped")
05    Mouse.Button1Down:Connect(function()
06        print("Tool was clicked")
07        Tool.Light.Material = "Neon"
08        Tool.Light.SpotLight.Enabled = true
09        else
10            Tool.Light.Material = "Metal"
11            Tool.Light.SpotLight.Enabled = false
12end)
13end)
14 
15Tool.Unequipped:Connect(function()
16    print("Tool was unequipped")
17    Tool.Light.Material = "Metal"
18    Tool.Light.SpotLight.Enabled = false
19end)

thanks!

1
You wrote 'else' but you didn't write 'if'. rabbi99 714 — 5y
0
do i put "if then"? Fxding_cam 60 — 5y
0
or would i put if the mouse is pressed the flashlight will turn off? Fxding_cam 60 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

It seems like you added an elsein your script but it doesnt seem like theres an if before it. I fixed it for you:

01local Tool = script.Parent
02 
03Tool.Equipped:Connect(function(Mouse)
04    print("Tool was equipped")
05 
06    Mouse.Button1Down:Connect(function()
07        if Tool.Light.SpotLight.Enabled == false then
08                print("Tool was clicked")
09                Tool.Light.Material = "Neon"
10                Tool.Light.SpotLight.Enabled = true
11            else
12            Tool.Light.Material = "Metal"
13            Tool.Light.SpotLight.Enabled = false
14        end
15    end)
View all 23 lines...
0
this worked! Fxding_cam 60 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

I might be wrong, I'm fairly new to coding myself, but I think the code should look like this

01local Tool = script.Parent
02 
03Tool.Equipped:Connect(function(Mouse)
04    print("Tool was equipped")
05    Mouse.Button1Down:Connect(function()
06        print("Tool was clicked")
07        if Tool.Light.Material == "Neon"--if the tool is on
08            Tool.Light.Material = "Metal"
09            Tool.Light.SpotLight.Enabled = false--turn the tool off
10        elseif Tool.Light.Material == "Metal"--else if the tool is off
11            Tool.Light.Material = "Neon"
12            Tool.Light.SpotLight.Enabled = true--turn the tool on
13        end
14    end)
15end)
View all 21 lines...

This way it would check if the light is off, and turn it on if it's clicked, or if it's off, when it's clicked turn the flashlight on.

0
i got Expected 'then' when parsing if statement, got '=' but i know how to fix it Fxding_cam 60 — 5y
0
oh yeah, i forgot to put double '==' sorry about that jediplocoon 877 — 5y

Answer this question