Code Review for Emergency Vehicle Lighting and Siren
I’ve created an ELS (Emergency Lighting System) for my vehicles. I’m not sure why, but something about this code seems buggy and/or like it could be optimized. I’m looking to make my code clean.
I’ve tried fake ternaries, but I always seem to run into problems with them. I don’t think I’m writing them properly, so I just remove them from my code.
If anyone has suggestions or feedback, it would be greatly appreciated. Thank you!
(Context: The GUI referenced in the code below is displayed only to the person in the vehicle seat.)
local pattern = "Off"
local siren = "Off"
local airhorn = false
local traffic = "Off"
local takedown = false
local alley = false
local car = workspace["2018 Ford Police Interceptor Utility"].Body
local sirens = car.ELS.Siren
--[[TEXT SCALING]]
SmartScale:NewGroup{
TextObjects = {
ContentFrame.Lights.TitleLabel,
ContentFrame.Lights.ContentLabel,
ContentFrame.Siren.TitleLabel,
ContentFrame.Siren.ContentLabel,
ContentFrame.Traffic.TitleLabel,
ContentFrame.Traffic.ContentLabel,
ContentFrame.Floods.TitleLabel,
ContentFrame.Floods.ContentLabel
},
OnlyUpdateWhenVisible = true,
UpdateWhenMadeVisible = true
}
SmartScale:NewGroup{
TextObjects = {
TitleFrame.TitleLabel
},
OnlyUpdateWhenVisible = true,
UpdateWhenMadeVisible = true,
}
--[[FUNCTIONS]]
function checkIfSirenPlaying(sirenArg)
if sirenArg == "Wail" then
if sirens.Wail.IsPlaying == true then
guiModify("Siren",string.upper("OFF"))
siren = "Off"
else
guiModify("Siren","WAIL")
siren = "Wail"
end
elseif sirenArg == "Yelp" then
if sirens.Yelp.IsPlaying == true then
guiModify("Siren",string.upper("OFF"))
siren = "Off"
else
guiModify("Siren","YELP")
siren = "Yelp"
end
elseif sirenArg == "Phaser" then
if sirens.Phaser.IsPlaying == true then
guiModify("Siren",string.upper("OFF"))
siren = "Off"
else
guiModify("Siren","PHASER")
siren = "Phaser"
end
elseif sirenArg == "Off" then
guiModify("Siren","OFF")
siren = "Off"
end
end
function userInputBegan(i,gp)
if not gp then
if i.KeyCode == Enum.KeyCode.J then
if pattern == "Pattern 1" then
pattern = "Pattern 2"
elseif pattern == "Pattern 2" then
pattern = "Off"
elseif pattern == "Off" then
pattern = "Pattern 1"
end
car.ELS.Events.LightsEvent:FireServer(pattern)
guiModify("Lights",string.upper(pattern))
elseif i.KeyCode == Enum.KeyCode.R then
car.ELS.Events.SirenEvent:FireServer("Wail")
checkIfSirenPlaying("Wail")
elseif i.KeyCode == Enum.KeyCode.F then
car.ELS.Events.SirenEvent:FireServer("AirhornOn")
guiModify("Siren","AIRHORN")
elseif i.KeyCode == Enum.KeyCode.V then
car.ELS.Events.SirenEvent:FireServer("Phaser")
checkIfSirenPlaying("Phaser")
elseif i.KeyCode == Enum.KeyCode.G then
car.ELS.Events.SirenEvent:FireServer("Yelp")
checkIfSirenPlaying("Yelp")
elseif i.KeyCode == Enum.KeyCode.B then
if traffic == "Left" then
traffic = "Right"
car.ELS.Events.TrafficEvent:FireServer("Right")
elseif traffic == "Right" then
traffic = "Split"
car.ELS.Events.TrafficEvent:FireServer("Split")
elseif traffic == "Split" then
traffic = "Off"
car.ELS.Events.TrafficEvent:FireServer("Off")
elseif traffic == "Off" then
traffic = "Left"
car.ELS.Events.TrafficEvent:FireServer("Left")
end
guiModify("Traffic",string.upper(traffic))
elseif i.KeyCode == Enum.KeyCode.N then
car.ELS.Events.FloodEvent:FireServer("Takedown")
takedown = not takedown
if takedown and alley then
guiModify("Floods","ALL")
elseif takedown then
guiModify("Floods",string.upper("Takedown"))
elseif not takedown and alley then
guiModify("Floods",string.upper("Alley"))
else
guiModify("Floods",string.upper("Off"))
end
elseif i.KeyCode == Enum.KeyCode.M then
car.ELS.Events.FloodEvent:FireServer("Alleys")
alley = not alley
if takedown and alley then
guiModify("Floods","ALL")
elseif takedown then
guiModify("Floods",string.upper("Takedown"))
elseif not takedown and alley then
guiModify("Floods",string.upper("Alley"))
else
guiModify("Floods",string.upper("Off"))
end
end
end
end
function userInputEnded(i,gp)
if i.KeyCode == Enum.KeyCode.F then
car.ELS.Events.SirenEvent:FireServer("AirhornOff")
guiModify("Siren",string.upper(siren))
end
end
function guiModify(frame,value)
ContentFrame[frame].ContentLabel.Text = value
if value == "OFF" then
ContentFrame[frame].ContentLabel.TextColor3 = Color3.fromRGB(198, 40, 40)
else
ContentFrame[frame].ContentLabel.TextColor3 = Color3.fromRGB(0, 200, 83)
end
end
--[[KEYBIND CONNECTIONS]]
UIS.InputBegan:Connect(userInputBegan)
UIS.InputEnded:Connect(userInputEnded)
``