Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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)

0
Please use a codeblock to put code in. AlexanderYar 788 — 3y

4 answers

Log in to vote
0
Answered by 3 years ago

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

0
thx for your comments but I figured it out Leviathan458 2 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

If switch == 0 then switch == 1

0
Sorry for my typo but remove one = at the second Bankrovers 226 — 3y
Log in to vote
0
Answered by 3 years ago

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)
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. DarkageMast3r 140 — 3y
Log in to vote
0
Answered by 3 years ago

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.

Answer this question