ive made some code to change the range of a point light the first half works but when it gets to the else if it stops working
here's the code: 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)
I'm not sure if this is gonna fix it, but I think you might have to specify what "switch" actually does
If switch == 0 then switch == 1
You have the right idea but you have made a few errors, I fixed them all, hopefully this helps.
And make sure it's a script
and not localscript
local Clickdetector = script.Parent.ClickDetector local Switch = YourValueHere.Value -- create a Value in ReplicatedStorage Clickdetector.MouseClick:Connect(function(click) if Switch == 0 then game.Workspace.MeshPart.PointLight.Range = 0 Switch = 1 elseif Switch == 1 then game.Workspace.MeshPart.PointLight.Range = 12 switch = 0 end end)
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.