4
\$\begingroup\$

I got bored and decided to create a lua announcement system with the ROBLOX API.

One part is the display code, and the other part is the announcement code.

DISPLAY CODE

while true do
    wait(0.2)
    if script.Parent.Parent.An.Value == 0 then
        script.Parent.An1.Text = "This bus terminates here"
        script.Parent.An2.Text = "Bus is on diversion"
        script.Parent.Selected.Text = "The destination has changed"
        script.Parent.An3.Text = "Driver change"
        script.Parent.An4.Text = "Next stop is closed"
        script.Parent.An5.Text = "Move Down"

    elseif script.Parent.Parent.An.Value == 1 then
        script.Parent.An1.Text = "Next stop is closed"
        script.Parent.An2.Text = "This bus terminates here"
        script.Parent.Selected.Text = "Bus is on diversion"
        script.Parent.An5.Text = "Move Down"
        script.Parent.An3.Text = "The destination has changed"
        script.Parent.An4.Text = "Driver change"

    elseif script.Parent.Parent.An.Value == 2 then
        script.Parent.An5.Text = "Move Down"
        script.Parent.An1.Text = "Driver change"
        script.Parent.An2.Text = "Next stop is closed"
        script.Parent.Selected.Text = "This bus terminates here"
        script.Parent.An3.Text = "Bus is on diversion"
        script.Parent.An4.Text = "The destination has changed"

    elseif script.Parent.Parent.An.Value == 3 then
        script.Parent.An1.Text = "The destination has changed"
        script.Parent.An2.Text = "Driver change"
        script.Parent.Selected.Text = "Next stop is closed"
        script.Parent.An3.Text = "This bus terminates here"
        script.Parent.An4.Text = "Bus is on diversion"
        script.Parent.An5.Text = "Move Down"

    elseif script.Parent.Parent.An.Value == 4 then
        script.Parent.An1.Text = "Bus is on diversion"
        script.Parent.An2.Text = "The destination has changed"
        script.Parent.Selected.Text = "Move Down"
        script.Parent.An3.Text = "Next stop is closed"
        script.Parent.An4.Text = "This bus terminates here"
        script.Parent.An5.Text = "Driver Change"

    elseif script.Parent.Parent.An.Value == 5 then
        script.Parent.An1.Text = "Bus is on diversion"
        script.Parent.An2.Text = "The destination has changed"
        script.Parent.Selected.Text = "Driver Change"
        script.Parent.An3.Text = "Next stop is closed"
        script.Parent.An4.Text = "This bus terminates here"

    elseif script.Parent.Parent.An.Value == 6 then
        script.Parent.An1.Text = "Bus is on diversion"
        script.Parent.An2.Text = "The destination has changed"
        script.Parent.Selected.Text = "Passenger Safety"
        script.Parent.An3.Text = "Driver Change"
        script.Parent.An4.Text = "This bus terminates here"

    elseif script.Parent.Parent.An.Value == 7 then
        script.Parent.An1.Text = "Bus is on diversion"
        script.Parent.An2.Text = "The destination has changed"
        script.Parent.Selected.Text = "No Standing On The Upper Deck"
        script.Parent.An3.Text = "Driver Change"
        script.Parent.An4.Text = "This bus terminates here"

    elseif script.Parent.Parent.An.Value == 8 then
        script.Parent.An1.Text = "Bus is on diversion"
        script.Parent.An2.Text = "The destination has changed"
        script.Parent.Selected.Text = "Ticket Inspectors"
        script.Parent.An3.Text = "Driver Change"
        script.Parent.An4.Text = "This bus terminates here"

    elseif script.Parent.Parent.An.Value == 9 then
        script.Parent.An1.Text = "Bus is on diversion"
        script.Parent.An2.Text = "The destination has changed"
        script.Parent.Selected.Text = "Please leave the bus"
        script.Parent.An3.Text = "Driver Change"
        script.Parent.An4.Text = "This bus terminates here"

    elseif script.Parent.Parent.An.Value == 10 then
        script.Parent.An1.Text = "Bus is on diversion"
        script.Parent.An2.Text = "The destination has changed"
        script.Parent.Selected.Text = "Regulating Service"
        script.Parent.An3.Text = "Driver Change"
        script.Parent.An4.Text = "This bus terminates here"

    elseif script.Parent.Parent.An.Value == 11 then
        script.Parent.Parent.An.Value = 0

    elseif script.Parent.Parent.An.Value == -1 then
        script.Parent.Parent.An.Value = 11
    end
end

ANNOUNCEMENT DISPLAY CODE

c = script.Parent.Parent.Parent:GetChildren()



function onClicked()
if script.Parent.Parent.iBusControlSystem.iBus.Enabled == true then 

    if script.Parent.Parent.iBusControlSystem.An.Value == 0 then
        script.Parent.Parent.iBusControlSystem.DestinationChange:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "The destination has changed. Listen for more info"
        wait(7)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "The destination of this bus has changed"


            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end

    elseif script.Parent.Parent.iBusControlSystem.An.Value == 1 then
        script.Parent.Parent.iBusControlSystem.BusOnDiversion:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "This bus is now on Diversion. Please listen for more info"
        wait(7)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "This bus is on Diversion"
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end

    elseif script.Parent.Parent.iBusControlSystem.An.Value == 2 then
        script.Parent.Parent.iBusControlSystem.Terminate:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "This bus terminates here, take your items with you"
        wait(7)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Bus Terminates Here"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "This bus terminates here, Please take all your belongings with you."
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end

    elseif script.Parent.Parent.iBusControlSystem.An.Value == 3 then
        script.Parent.Parent.iBusControlSystem.NextStopClosed:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "The next bus stop is closed"
        wait(7)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "The next bus stop is closed"
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end

elseif script.Parent.Parent.iBusControlSystem.An.Value == 4 then
        script.Parent.Parent.iBusControlSystem.MoveDown:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Please move down inside the bus"
        wait(7)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "Move Down Inside The Bus"
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end     

    elseif script.Parent.Parent.iBusControlSystem.An.Value == 5 then
        script.Parent.Parent.iBusControlSystem.DriverChange:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Bus will wait here while drivers are changed"
        wait(7)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "Wait a short time for change of drivers."
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end     


    elseif script.Parent.Parent.iBusControlSystem.An.Value == 6 then
        script.Parent.Parent.iBusControlSystem.PassengerSafety:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "For safety, drivers cannot open doors between stops"
        wait(7)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "In the intrest of passenger safety drivers cannot open doors"
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end



    elseif script.Parent.Parent.iBusControlSystem.An.Value == 7 then
        script.Parent.Parent.iBusControlSystem.Standing:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "No Standing On The Upper Deck"
        wait(1.91)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Or Stairs Please"
        wait(2.49)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "Wait a short time for change of drivers."
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end     

    elseif script.Parent.Parent.iBusControlSystem.An.Value == 8 then
        script.Parent.Parent.iBusControlSystem.Tickets:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Ticket inspectors operate on this bus"
        wait(2.65)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Pay as you go users are reminded to"
        wait(2)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "touch in your oyster card when you board"
        wait(5.84)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "Wait a short time for change of drivers."
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end     

    elseif script.Parent.Parent.iBusControlSystem.An.Value == 9 then
        script.Parent.Parent.iBusControlSystem.LeaveBus:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Please leave the bus"
        wait(1.21)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "And board the next avaliable bus"
        wait(3)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "Wait a short time for change of drivers."
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end

    elseif script.Parent.Parent.iBusControlSystem.An.Value == 10 then
        script.Parent.Parent.iBusControlSystem.Regulate:Play()
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "This bus is being held here"
        wait(2)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "in order to help regulate the service"

        wait(7)
        script.Parent.Parent.Interface.SurfaceGui.Frame.Announcement.Text = "Welcome aboard"
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = true
                c[i].SurfaceGui.iBus.Text = "Wait a short time for change of drivers."
            end
        end
        wait(10)
        for i=1, #c do
            if c[i].Name == "iBusPassenger" then
                c[i].SurfaceGui.iBus.Visible = false
                c[i].SurfaceGui.iBus.Text = "Label"
            end
        end 
    end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

What do you think?

\$\endgroup\$
2
\$\begingroup\$

DRY

You are repeating yourself a lot in your code. Here is just one example:

if script.Parent.Parent.An.Value == 0 then
    script.Parent.An1.Text = "This bus terminates here"
    script.Parent.An2.Text = "Bus is on diversion"
    script.Parent.Selected.Text = "The destination has changed"
    script.Parent.An3.Text = "Driver change"
    script.Parent.An4.Text = "Next stop is closed"
    script.Parent.An5.Text = "Move Down"

elseif script.Parent.Parent.An.Value == 1 then
    script.Parent.An1.Text = "Next stop is closed"
    script.Parent.An2.Text = "This bus terminates here"
    script.Parent.Selected.Text = "Bus is on diversion"
    script.Parent.An5.Text = "Move Down"
    script.Parent.An3.Text = "The destination has changed"
    script.Parent.An4.Text = "Driver change"

elseif script.Parent.Parent.An.Value == 2 then
    script.Parent.An5.Text = "Move Down"
    script.Parent.An1.Text = "Driver change"
    script.Parent.An2.Text = "Next stop is closed"
    script.Parent.Selected.Text = "This bus terminates here"
    script.Parent.An3.Text = "Bus is on diversion"
    script.Parent.An4.Text = "The destination has changed"
...

A big reason I see for this is these AnN things. If possible, try grouping things like these into arrays so you can easily iterate through them with loops, giving you the ability to work on the entire array with only a few lines, rather than several groups of a few lines.


DRY... again!

Along with those AnN variables, I see this in all places of your code:

if thing == 0 then

elseif thing == 1 then

elseif thing == 2 then

...

Then, in each block, very similar code is posted. This is, again, repeating yourself and should be avoided.

Try to fit the code in the blocks into a loop. If that means also creating an array of the different "bus messages", then okay. Then, as you are looping, you can use the index to compute which bus message to use.

Overall, you are really doing a lot of repeating in this code and that is not good.


Save your stuff

This ignores the above tip for now.

You are accessing this a lot:

script.Parent.AnN

This type of access, when overused as it is here, slows down your code. Store these sorts of things in variables:

local parent = script.Parent
local An1 = parent.An1
...

(if you follow the above tip, these AnN variables will probably be in an array instead).

Now, you are doing much less constant field access, overall speeding up your code.

\$\endgroup\$
1
\$\begingroup\$

In support of SirPython's original answer, I'd suggest creating a table populated with the replies. Then, use a function to lookup and assign those values:

local tLookup = {
    [0] = {
        An1 = "This bus terminates here",
        An2 = "Bus is on diversion",
        An3 = "Driver change",
        An4 = "Next stop is closed",
        An5 = "Move Down",
        Selected = "The destination has changed",
    }
    .
    .
    .
}
function SetValues( tIn, tVal )
    for Key, Value in pairs(tVal) do
        tIn[Key].Text = Value
    end
end

and, the current if..elseif..else block will be reduced to:

if tLookup[script.Parent.Parent.An.Value] then
    SetValues( script.Parent, tLookup[script.Parent.Parent.An.Value] )
end
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.