Get UniqueID of a Player

Hey,

I want that the unique ID is printed into the chat, when someone types in “!uniqueid”

This is my code, which is not working:


function uniqueIdCommand()
		    if (text == "!uniqueid") then 
			    chat.AddText( Color( 100, 100, 255 ), ", Your Unique ID: ", Color( 100, 255, 100 ), LocalPlayer():UniqueID() )
	            end
end

It would be very nice, if someone could correct the code :slight_smile:

text is nil and you’re not calling your function.


function uniqueIdCommand( pl, text, teamonly )
		    if (text == "!uniqueid") then 
			    chat.AddText( Color( 100, 100, 255 ), ", Your Unique ID: ", Color( 100, 255, 100 ), pl:UniqueID() )
	            end
end

hook.Add( "PlayerSay", "Chat", uniqueIdCommand )

Is also not working

I recommend you to better use the SteamID64 or SteamID.

Myself i never use the UniqueID function. I always use SteamID or SteamID64.

Anyway, you asked for code correction, i will do it for you



--You want a console command
concommand.Add("print_uniqueid", function()
	if (IsValid(LocalPlayer())) then
		uniqueIdCommand()
	end
end)
--End

--You want a chat command
if SERVER then
	util.AddNetworkString("AddonName_PlyUniqueID")
	hook.Add("PlayerSay", "AddonName_PlyUniqueID", function(ply, txt, flag)
		if (txt == "!uniqueid") then
			--I know 2 ways now :
			//using ply:SendLua(), warning cheaters like when you do this
			net.Start("AddonName_PlyUniqueID") //Prefer that
				net.WriteInt(ply:UniqueID(), 32)
			net.SendToServer()
		end
	end)
elseif CLIENT then
	net.Receive("AddonName_PlyUniqueID", function(len, ply)
		if (IsValid(LocalPlayer())) then
			chat.AddText(Color(100, 100, 255), "Your Unique ID: ", Color(100, 255, 100), net.ReadInt(32))
		end
	end)
end
--End

function uniqueIdCommand()
    chat.AddText(Color(100, 100, 255), "Your Unique ID: ", Color(100, 255, 100), LocalPlayer():UniqueID())
end


Thank you very much, its working! :smile:

I see that someone replied, and that you got it working. But for the purpose of explanation:

GM/PlayerSay

PlayerSay is a server-side-only hook.