continueButton.DoClick = function()
local plySelected = plyList:GetOptionText(plyList:GetSelectedID())
print(plySelected)
end
As you can see there’s a “plyList” panel, which is a DComboBox. The names of the current players online are in the list. However, I’d like it to visibly show the players’ names but when selected print their SteamID. How would I go about doing so?
I know it’s confusing, but I assure you, there’s valid reasoning behind all of this.
If the only thing stored in the ComboBox is the player’s name, you could iterate over player.GetAll() to compare their names with the selected one. Once you have the player, you can get their ID.
If you only have the player’s name, you can only get the player object by comparing the names, unless you have a table storing all player objects and the ComboBox element number as key. That way every ComboBox option is directly linked with a player, but if two players have the same name, you won’t be able to tell which one you want to report.
playerstable = player:GetAll()
local DComboBox = vgui.Create( "DComboBox" )
DComboBox:SetValue( "Players" )
for _,v in pairs(playerstable) do
DComboBox:AddChoice(v:GetName())
end
continueButton.DoClick = function()
local plySelected = playerstable[DComboBox:GetSelectedID()]:SteamID()
print(plySelected)
end
It’s pretty basic, and it probably won’t work if you copy and paste, because SteamID() must be called serverside, and the combobox is clientside, so you’ll have to send a message to the server with the selected option or something like that. Also, I’m not sure if combobox ID’s start from 0 or 1. Perhaps there’s another way to do it.
It’s in the documentation for DComboBox, you can have multiple arguments for AddChoice, which will then get passed in OnSelect, for example. You can also use GetOptionData.
local dc = vgui.Create("DComboBox")
for _,v in pairs(player.GetAll()) do
dc:AddChoice(v:Name(), v:SteamID())
end
continueButton.DoClick = function()
local ply = dc:GetOptionData(dc:GetSelectedID())
print(ply)
end
The Player:SteamID() wiki page says “For Bots this will return “BOT” on the server and on the client it returns “NULL”.”
I thought it always returned NULL on client, but I guess that’s only for bots.
Amm, yes you can? Each element in a table has a key. If the player’s key is the same as the combobox choice ID, you can access it directly. But TylerB solution is better. I didn’t know you could store hidden data inside a combobox choice.