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.
01 | local Tool = script.Parent |
02 |
03 | Tool.Equipped:Connect( function (Mouse) |
04 | print ( "Tool was equipped" ) |
05 | Mouse.Button 1 Down: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 |
12 | end ) |
13 | end ) |
14 |
15 | Tool.Unequipped:Connect( function () |
16 | print ( "Tool was unequipped" ) |
17 | Tool.Light.Material = "Metal" |
18 | Tool.Light.SpotLight.Enabled = false |
19 | end ) |
thanks!
It seems like you added an else
in your script but it doesnt seem like theres an if
before it.
I fixed it for you:
01 | local Tool = script.Parent |
02 |
03 | Tool.Equipped:Connect( function (Mouse) |
04 | print ( "Tool was equipped" ) |
05 |
06 | Mouse.Button 1 Down: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 ) |
I might be wrong, I'm fairly new to coding myself, but I think the code should look like this
01 | local Tool = script.Parent |
02 |
03 | Tool.Equipped:Connect( function (Mouse) |
04 | print ( "Tool was equipped" ) |
05 | Mouse.Button 1 Down: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 ) |
15 | end ) |
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.