Lets just say I wanted to find out if the player won the race or not, and you are scripting the UI. The common solution would be to script something like this:
game.ReplicatedStorage.SendScore.OnClientEvent:Connect(function(Win)
if Win == "Win" then
script.Parent.Text = "You beat your previous time"
script.Parent.Parent.Music.SoundId = "rbxassetid://6808979425"
elseif Win == "Loose" then
script.Parent.Text = "You didn't beat your previous time"
script.Parent.Parent.Music.SoundId = "rbxassetid://6784305960"
end
script.Parent.Parent.Visible = true
script.Parent.Parent.Music:Play()
end)
But lets just say that this is not the most efficient solution, and you will learn later on that doing this will be very inefficient, especially if you have like 10 different possible outcomes. The solution to this would be to use a table that will store the winning message, and another table that would store the sound ID.
How to shrink the code?
- Create the table(s). If you don’t know what a table is or how to use it, you can read this Developer Hub article:
local Messages = {Win = "You beat your previous time", Loose = "You didn't beat your previous time"}
local Music = {Win = "rbxassetid://6808979425", Loose = "rbxassetid://6784305960"}
- Now watch this! We will now shrink the code by getting a value in the table!
local Status = "Win" -- This will be the default outcome here.
game.ReplicatedStorage.SendScore.OnClientEvent:Connect(function(Win)
script.Parent.Text = Messages[Status]
script.Parent.Parent.Music.SoundId = Music[Status]
script.Parent.Parent.Visible = true
script.Parent.Parent.Music:Play()
end)
If you had 10 elseif’s then, it would be a massive improvement.
Full code:
local Messages = {Win = "You beat your previous time", Loose = "You didn't beat your previous time"}
local Music = {Win = "rbxassetid://6808979425", Loose = "rbxassetid://6784305960"}
game.ReplicatedStorage.SendScore.OnClientEvent:Connect(function(Win)
script.Parent.Text = Messages[Win]
script.Parent.Parent.Music.SoundId = Music[Win]
script.Parent.Parent.Visible = true
script.Parent.Parent.Music:Play()
end)
Now lets get a better example with at least 10 elseif’s.
I created this random game, that lets people press a button, and when they press the button, it shows on the text label the value.
However, I made them say three instead of 3, so here is the code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
ReplicatedStorage.ButtonPressed.OnServerEvent:Connect(function(Player, TheButtonsText)
if TheButtonsText == "1" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed one.")
elseif TheButtonsText == "2" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed two.")
elseif TheButtonsText == "3" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed three.")
elseif TheButtonsText == "4" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed four.")
elseif TheButtonsText == "5" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed five.")
elseif TheButtonsText == "6" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed six.")
elseif TheButtonsText == "7" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed seven.")
elseif TheButtonsText == "8" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed eight.")
elseif TheButtonsText == "9" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed nine.")
elseif TheButtonsText == "10" then
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed ten.")
end
end)
Woah there… That is a lot of elseifs, making it 25 lines of code. If you were to hire a professional scripture, it might take them a while to read that, and they might demand higher pays due to the pain (As I read through portfolios, they don’t like working with code like this.) Now lets simplify this code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local NumberTable = {
["1"] = "One",
["2"] = "Two",
["3"] = "Three",
["4"] = "Four",
["5"] = "Five",
["6"] = "Six",
["7"] = "Seven",
["8"] = "Eight",
["9"] = "Nine",
["10"] = "Ten",
}
ReplicatedStorage.ButtonPressed.OnServerEvent:Connect(function(Player, TheButtonsText)
ReplicatedStorage.DaAllClients:FireAllClients(Player.Name.. " Pressed ".. NumberTable[TheButtonsText])
end)
This here is only 17 lines of code, and it a lot more organized, plus, if you wanted to, you can make the table only 1 line! Professional scripters (like me) like working with this code rather than the elseif spam.
So long story short, use tables and not elseif. If you would like me to convert your code that uses elseif to code that uses tables, hit this topic with a reply!
Still confused? Feel free to make a reply and I will hopefully help you, and others! I was confused with tables and used to code with elseif.
Happy coding!
-WE