Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

3 comments

1

You wrote 'else' but you didn't write 'if'.

0

do i put "if then"?

0

or would i put if the mouse is pressed the flashlight will turn off?

2 answers

✓ Accepted answer

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:

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)

1 comment

0

this worked!

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.

2 comments

0

i got Expected 'then' when parsing if statement, got '=' but i know how to fix it

0

oh yeah, i forgot to put double '==' sorry about that

This discussion is preserved as it was. New answers, comments, and votes are closed.