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 4 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.

local Tool = script.Parent

Tool.Equipped:Connect(function(Mouse)
    print("Tool was equipped")
    Mouse.Button1Down:Connect(function()
        print("Tool was clicked")
        Tool.Light.Material = "Neon"
        Tool.Light.SpotLight.Enabled = true
        else
            Tool.Light.Material = "Metal"
            Tool.Light.SpotLight.Enabled = false
end)
end)

Tool.Unequipped:Connect(function()
    print("Tool was unequipped")
    Tool.Light.Material = "Metal"
    Tool.Light.SpotLight.Enabled = false
end)

thanks!

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

2 answers

Log in to vote
1
Answered by 4 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:

local Tool = script.Parent

Tool.Equipped:Connect(function(Mouse)
    print("Tool was equipped")

    Mouse.Button1Down:Connect(function()
        if Tool.Light.SpotLight.Enabled == false then
                print("Tool was clicked")
                Tool.Light.Material = "Neon"
                Tool.Light.SpotLight.Enabled = true
            else
            Tool.Light.Material = "Metal"
            Tool.Light.SpotLight.Enabled = false
        end
    end)
end)

Tool.Unequipped:Connect(function()
    print("Tool was unequipped")

    Tool.Light.Material = "Metal"
    Tool.Light.SpotLight.Enabled = false
end)
0
this worked! Fxding_cam 60 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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

local Tool = script.Parent

Tool.Equipped:Connect(function(Mouse)
    print("Tool was equipped")
    Mouse.Button1Down:Connect(function()
        print("Tool was clicked")
        if Tool.Light.Material == "Neon"--if the tool is on
            Tool.Light.Material = "Metal"
            Tool.Light.SpotLight.Enabled = false--turn the tool off
        elseif Tool.Light.Material == "Metal"--else if the tool is off
            Tool.Light.Material = "Neon"
            Tool.Light.SpotLight.Enabled = true--turn the tool on
        end
    end)
end)

Tool.Unequipped:Connect(function()
    print("Tool was unequipped")
    Tool.Light.Material = "Metal"
    Tool.Light.SpotLight.Enabled = false
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.

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

Answer this question