How to use Player:SteamID() in DComboBox?

So, I have the following code:



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.

Thanks to anyone who can help me!

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.

It’s still not precise… Due to the fact that they’re just names. It needs to be so precise, because it’s a player reporting system.

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.

In that case, how would I go about storing players’ SteamIDs invisibly within the DComboBox?

Something like this:



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


Huh? ply.SteamID is shared.

No offense, but I’m not so sure that’d work. You can’t call random keys within a table.

This too. You’re using : and . incorrectly, primarily in the player.GetAll() line.

I think I’m going to go commit suicide now. I’m blind.

Thanks so much, man! I can’t believe I didn’t see it, though. Haha

No problemo, also, I fixed it in my post but your quote has a misspelled fuction on line 4. (AddVoice instead of AddChoice :disgust:)

Yeah, I noticed it. Haha

I fixed it within the actual script, haha.

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.