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

whats wrong with my if statements and variables?

Asked by 4 years ago
Edited by BlackOrange3343 4 years ago

I'm not sure what is wrong. this is my first time doing scripting in lua but i know python. The code works except the if statements and i think there is a problem when i try to change "switch" between true and false. The code changes the part "load" to either transparent and non collide or solid and collide depending on what switch is.

here is the code:

switch = true
if swtich = true
    function onClicked()
    script.Parent.Parent.load.Transparency = 1
    script.Parent.Parent.load.CanCollide = false
    swtich = false
    end
    script.Parent.ClickDetector.MouseClick:connect (onClicked)


if swtich = false then  
    function onClicked()
    script.Parent.Parent.load.Transparency = 0
    script.Parent.Parent.load.CanCollide = true
    swtich = true
    end
    script.Parent.ClickDetector.MouseClick:connect (onClicked)

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Alright, so I've helped you place your code into a code block, so now let's go over a couple things:

  1. you spelled switch wrong on line 11 and line 2

  2. you need end's as well as then

Here's an example of a conditional statement in use:

local banana = true

if banana == true then
    print("Hello")
end -- this is what you're missing

If placed into any script it will print:

Hello

So, how do you fix the script? Well we can start by creating proper indention and making sure we have the correct amount of end's

local switch = true -- local variables are preferred in this case

if switch then-- this is similar to if switch == true then

end

if not switch then -- this is similar to if switch == false then

end

Next, let's have the function outside so that it's we don't need that many:

local switch = true -- local variables are preferred in this case

local function onClicked()
    if switch then
        script.Parent.Parent.load.Transparency = 1
        script.Parent.Parent.load.CanCollide = false
        switch = false
    elseif not switch then
        script.Parent.Parent.load.Transparency = 0
        script.Parent.Parent.load.CanCollide = true
        switch = true
    end
end

if switch then-- this is similar to if switch == true then
    script.Parent.ClickDetector.MouseClick:Connect(onClicked)
end

if not switch then -- this is similar to if switch == false then
    script.Parent.ClickDetector.MouseClick:connect (onClicked)
end

Now you might notice that we did the condition twice which is unnecessary so:

local switch = true -- local variables are preferred in this case

local function onClicked()
    if switch then
        script.Parent.Parent.load.Transparency = 1
        script.Parent.Parent.load.CanCollide = false
        switch = false
    elseif not switch then
        script.Parent.Parent.load.Transparency = 0
        script.Parent.Parent.load.CanCollide = true
        switch = true
    end
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

This is all we need!

Hope this helped! Best of luck!

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

When assigning a value to a variable you use one =, when checking the value of a variable you do ==. This is what I think is causing your code to error

You also spelt switch wrong at each if statement.

for example this is incorrect

var = 1

if var = 1 then
 print("hello')
end

however, this is correct.

var = 1

if var == 1 then
 print("hello')
end

Answer this question