Skip to content

Instantly share code, notes, and snippets.

@Adidea
Created August 4, 2015 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adidea/05de5e943a54f36efcb9 to your computer and use it in GitHub Desktop.
Save Adidea/05de5e943a54f36efcb9 to your computer and use it in GitHub Desktop.
--[[ LOCAL SCRIPT ]]--
local Dropdown = require(game.ReplicatedStorage.Dropdown)
local Gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
local TestList = Dropdown.Create({"one","two","three","four","five"}, Gui)
TestList.Button.Position = UDim2.new(0.5,-50,0.5,-25)
TestList.OnSelect(function(value)
print("Selected: ", value)
end)
--[[ DROPDOWN MODULE ]]--
Dropdown = {}
function Dropdown.Create(list, parent)
local newDropdown = {}
newDropdown.Button = Instance.new("TextButton")
newDropdown.Active = false
local sizeX, sizeY = 100, 50
newDropdown.Button.Size = UDim2.new(0,sizeX,0,sizeY)
newDropdown.Button.Text = list[1]
newDropdown.Button.Parent = parent
newDropdown.Listeners = {}
function newDropdown:HideList(bool)
if bool then
newDropdown.Button.ClipsDescendants = true
else
newDropdown.Button.ClipsDescendants = false
end
newDropdown.Active = not bool
end
newDropdown.Listeners.OnSelect = {}
function newDropdown.OnSelect(func)
-- fire when item on list is selected.
table.insert(newDropdown.Listeners.OnSelect, func)
end
function fireOnSelect(value)
for _,func in pairs(newDropdown.Listeners.OnSelect) do
func(value)
end
end
newDropdown.Button.MouseButton1Down:connect(function()
--toggle dropdown
if newDropdown.Active then
newDropdown:HideList(true)
else
newDropdown:HideList(false)
end
end)
for i = 1, #list do
local item = Instance.new("TextButton")
item.Size = UDim2.new(1,0,0,sizeY)
item.Position = UDim2.new(0,0,0,sizeY*(i))
item.Text = list[i]
item.Parent = newDropdown.Button
item.MouseButton1Down:connect(function()
newDropdown.Button.Text = list[i]
fireOnSelect(list[i])
newDropdown:HideList(true)
end)
end
newDropdown:HideList(true)
return newDropdown
end
return Dropdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment