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

Are multiple sequential "elseif" statments possible?

Asked by 5 years ago

This question is the result of me trying to edit a working script that was answered by Torren_Mr (thank you) with more unions but it ultimately just did either the same thing or nothing. Here's the script:

local ClickHere = script.Parent.Parent.ClickHere
local PlateEmp = script.Parent.Parent.PlateEmp
local PlateInv = script.Parent.Parent.PlateInv
local Plate1 = script.Parent.Parent.Plate1
local Plate2 = script.Parent.Parent.Plate2
local Plate3 = script.Parent.Parent.Plate3

function click()
    if PlateInv.Transparency == 1 then
        PlateInv.Transparency = 1
        PlateEmp.Transparency = 0
        Plate1.Transparency = 1
        Plate2.Transparency = 1
        Plate3.Transparency = 1
    elseif PlateEmp.Transparency == 0 then
        PlateInv.Transparency = 1
        PlateEmp.Transparency = 1
        Plate1.Transparency = 0
        Plate2.Transparency = 1
        Plate3.Transparency = 1
    elseif Plate1.Transparency == 0 then
        PlateInv.Transparency = 1
        PlateEmp.Transparency = 1
        Plate1.Transparency = 1
        Plate2.Transparency = 0
        Plate3.Transparency = 1
    elseif Plate2.Transparency == 0 then
        PlateInv.Transparency = 1
        PlateEmp.Transparency = 1
        Plate1.Transparency = 1
        Plate2.Transparency = 1
        Plate3.Transparency = 0
    elseif Plate3.Transparency == 0 then
        PlateInv.Transparency = 1
        PlateEmp.Transparency = 1
        Plate1.Transparency = 1
        Plate2.Transparency = 1
        Plate3.Transparency = 1
    end
    wait(2) 
end

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

Can there be multiple "elseif" statements in a script and can they be in a sequential, looping order?

I am trying to make this script turn all unions besides one invisible, and when the player clicks the Click Detector, the next union in the pattern transparency turns to 0 while the previous goes to 1 and vice versa with all the unions.

Also, if you look at the script from top to bottom, that is the pattern I am trying to complete. (With "PlateInv" always invisible or transparency = 1)

(PlateInv -> PlateEmp -> Plate1 -> Plate2 -> Plate3 -> Back to PlateInv)

Please be as specific and/or simple as you can since I am not good at scripting or the understanding of lua that well. Sorry for asking so many questions based one problem, I am just very confused.

0
If it works, then yeah. thesit123 509 — 5y

1 answer

Log in to vote
1
Answered by
sleazel 1287 Moderation Voter
5 years ago

There are some mistakes in your logic sequence, for example first if will always be true, so the next elseifs will never run. I will amend that for you:

local ClickHere = script.Parent.Parent.ClickHere
local PlateEmp = script.Parent.Parent.PlateEmp
local PlateInv = script.Parent.Parent.PlateInv
local Plate1 = script.Parent.Parent.Plate1
local Plate2 = script.Parent.Parent.Plate2
local Plate3 = script.Parent.Parent.Plate3

function click()
    if PlateInv.Transparency == 0 then --used to be 1
        PlateInv.Transparency = 1
        PlateEmp.Transparency = 0
        Plate1.Transparency = 1
        Plate2.Transparency = 1
        Plate3.Transparency = 1
    elseif PlateEmp.Transparency == 0 then
        PlateInv.Transparency = 1
        PlateEmp.Transparency = 1
        Plate1.Transparency = 0
        Plate2.Transparency = 1
        Plate3.Transparency = 1
    elseif Plate1.Transparency == 0 then
        PlateInv.Transparency = 1
        PlateEmp.Transparency = 1
        Plate1.Transparency = 1
        Plate2.Transparency = 0
        Plate3.Transparency = 1
    elseif Plate2.Transparency == 0 then
        PlateInv.Transparency = 1
        PlateEmp.Transparency = 1
        Plate1.Transparency = 1
        Plate2.Transparency = 1
        Plate3.Transparency = 0
    elseif Plate3.Transparency == 0 then
        PlateInv.Transparency = 0 -- used to be 1
        PlateEmp.Transparency = 1
        Plate1.Transparency = 1
        Plate2.Transparency = 1
        Plate3.Transparency = 1
    end
    wait(2) 
end

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

You may also simplify your script by adhering to DRY (don't repeat yourself) principle.

local ClickHere = script.Parent.Parent.ClickHere
local Plates = {
script.Parent.Parent.PlateEmp,
script.Parent.Parent.PlateInv,
script.Parent.Parent.Plate1,
script.Parent.Parent.Plate2,
script.Parent.Parent.Plate3
    }

for i = 1, #Plates, do
    Plates[i].Transparency = 1
end
currentPlate = 1
Plates[currentPlate].Transparency = 0

function click()
    Plates[currentPlate].Transparency = 1
    currentPlate = currentPlate +1
    if currentPlate > #Plates then
        currentPlate = 1
    end
    Plates[currentPlate].Transparency = 0
end

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

Have a nice scripting session.

0
sleazel, thank you so much for helping me fix this script, it works flawlessly :) ArkhamChingy 13 — 5y
Ad

Answer this question