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!
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)
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.