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

hello my light switch code isn't working please help?

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)

1 comment

0

Please use a codeblock to put code in.

4 answers

I'm not sure if this is gonna fix it, but I think you might have to specify what "switch" actually does

1 comment

0

thx for your comments but I figured it out

If switch == 0 then switch == 1

1 comment

0

Sorry for my typo but remove one = at the second

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)

1 comment

0

Why are you creating a new value in ReplicatedStorage simply to hold a 0 as value? Seems wasteful to me, because you're not even changing the value of the value object inside the script.

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.

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