To start off, please format your code.
I've done it for you, thank me later.
local Clickdetector = script.Parent.ClickDetector
local Switch = 0
Clickdetector.MouseClick:Connect(function(PlayerWhoClicked)
if Switch == 0 then
game.Workspace.MeshPart.PointLight.Range = 0
local Switch = Switch + 1
elseif Switch == 1 then
game.Workspace.MeshPart.PointLight.Range = 12
local switch = switch - 1
end
end)
There are about 2 problems:
First, the major problem, is that you;'re redefining the variable Switch.
Saying local X = 5 will create a new variable named X, and limits it to that scope.
Saying local Switch = Switch + 1 will create a new variable called Switch, and set it to the desired value (Switch + 1), but it will not overwrite the Switch variable that you're trying to overwrite.
To give an example of this:
local X = 5
do
local X = 10
print(X)
end
print(X)
Will print 10, 5
The simple fix is to simply remove the local before the variable declaration thingy:
local Clickdetector = script.Parent.ClickDetector
local Switch = 0
Clickdetector.MouseClick:Connect(function(PlayerWhoClicked)
if Switch == 0 then
game.Workspace.MeshPart.PointLight.Range = 0
Switch = Switch + 1
elseif Switch == 1 then
game.Workspace.MeshPart.PointLight.Range = 12
switch = switch - 1
end
end)
The second problem is that on line 11, you didn't start Switch with a capital, while you did start the first Switch variable with a capital.
To fix this:
local Clickdetector = script.Parent.ClickDetector
local Switch = 0
Clickdetector.MouseClick:Connect(function(PlayerWhoClicked)
if Switch == 0 then
game.Workspace.MeshPart.PointLight.Range = 0
Switch = Switch + 1
elseif Switch == 1 then
game.Workspace.MeshPart.PointLight.Range = 12
Switch = Switch - 1
end
end)
And then your code will work.
Bit weird that you're incrementing and decrementing Switch by 1, but it definitely works.
1 comment
Please use a codeblock to put code in.