I want it so if two people was in game, one name John (@John_bob) and the other named Steve (@steve_jobs) and John used :userid Steve, it would print Steve’s userid of 15654864 in the console which he can view, this isnt exactly what I want to do but if I can get this to happen I can do what is needed my self
I’m a bit confused by what you’re looking to do here.
Not entirely sure with your use case but could you use a module to store an accessible dictionary in which you map usernames to their actual user ids?
e.g:
local usernameHandler = {}
local userIds = {}
function usernameHandler:registerUser(player)
userIds[player.Name] = player.UserId
end
function usernameHandler:getUserIdFromDisplayName(displayName)
return userIds[displayName]
end
return usernameHandler
Again I don’t really know if this is what you want.
You can loop through the players in game, if they have the right display name, it will work, however, if multiple people have the same display name, it will only work on one, or all
local Players = game:GetService("Players")
local function playerFromDName(DName) -- one
for _, player in ipairs(Players:GetPlayers()) do
if player.DisplayName == DName then
return player
end
end
end
local function playersFromDName(DName) -- multiple
local results = {}
for _, player in ipairs(Players:GetPlayers()) do
if player.DisplayName == DName then
table.insert(results, player)
end
end
return results
end
You would want to do userIds[player.DisplayName] = player.UserId
, so that it uses the display name, and not username.
The question is: why? Display names aren’t unique identifiers like UserIds (a constant associated with an account) and usernames (only one account may have the username, but it may not be their current username). Display name should, as the name says, only be used for display purposes, not for anything else.
Even building this system yourself since it’s not natively supported would be a fair bit tricky because you want to directly go from a display name to a UserId. As per the aforementioned, display names are not unique, so there will be unresolvable collisions because you don’t have additional data that can help further identify them.
You should not be using display names for technical work.
The thread reads more like you want to be able to run commands on partial usernames rather than specifically display names given the usernames in use. That much is possible, you can check if any of the players in the server have their name beginning with the string passed to the command then print the player instance’s UserId.
Colbert (@colbert2677) is correct. Display names can be copied by anyone. However, it depends on what you’re trying to do.
Also to solve your problem:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message)
if Message == "userid" then
print(Player.UserId)
end
end)
end)
Because on my nametag system there is :tag command, and atm it only uses usernames, but I want to use display names as well, and if I can get the user ID from the display name I could probably make it work
If you want to make commands work with displayname just change .Name to DisplayName but take in mind that it will work with everyone who have same DisplayName that you provided
One sec I will send you the script that should work fine
local IdFromDisplayNameReturnsTable = true
local function IdFromDisplayName(DisplayName)
local Results = {}
for i,v in pairs(game.Players:GetChildren()) do
if v.Name:lower():sub(1,#DisplayName) == DisplayName:lower() then
if IdFromDisplayNameReturnsTable = false then
return v.UserId -- Returns First Player if you want to return table with players use
else
table.insert(Results,v.UserId)
end
end
end
return Results
end
Atm my code is this
local Server = require(rs.NameTagConfig)
if Server["Tag cmd"].Enabled == true then
if Server["Tag cmd"]["Minimum rank to use"] == nil then
error("You have the tag command enabled but havent set a minimum rank so the commands wont work")
elseif Server["Tag cmd"]["Minimum rank to use"] ~= nil then
local MinimumRankToUseCommand = Server["Tag cmd"]["Minimum rank to use"]
local GroupId = Server.GroupID
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
end)
Player.Chatted:Connect(function(Message)
local SplitMessage = Message:split(" ")
if SplitMessage[1] == Server["Tag cmd"].Prefix..Server["Tag cmd"].Command and Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
local NameOfPlayerToChange = SplitMessage[2]
local PlayerToChange = game.Workspace:FindFirstChild(NameOfPlayerToChange)
if NameOfPlayerToChange == "me" or NameOfPlayerToChange == "Me" then
PlayerToChange = game.Workspace:FindFirstChild(Player.Name)
end
local Reason = Message:split(NameOfPlayerToChange)[2]
local WarningGUI = PlayerToChange.Rank.Main.Special
WarningGUI.Text = Reason
end
end)
end)
end
end
So how would I change it to add Display name compatibility
I was gonna use user id because then from the user id I can get the username and it would change
you should make another function that tries to get player just like username and if it doesn’t find one it would try with displayname
But then would it not search for display name in the explorer to change the ui?
local Server = require(rs.NameTagConfig)
if Server["Tag cmd"].Enabled == true then
if Server["Tag cmd"]["Minimum rank to use"] == nil then
error("You have the tag command enabled but haven't set a minimum rank so the commands won't work")
elseif Server["Tag cmd"]["Minimum rank to use"] ~= nil then
local MinimumRankToUseCommand = Server["Tag cmd"]["Minimum rank to use"]
local GroupId = Server.GroupID
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
end)
Player.Chatted:Connect(function(Message)
local SplitMessage = Message:split(" ")
if SplitMessage[1] == Server["Tag cmd"].Prefix..Server["Tag cmd"].Command and Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
local PartialNameOfPlayerToChange = SplitMessage[2]
local PlayerToChange = nil
for _, p in pairs(game.Players:GetPlayers()) do
if p.Name:sub(1, #PartialNameOfPlayerToChange):lower() == PartialNameOfPlayerToChange:lower() then
PlayerToChange = p.Character
break
elseif p.DisplayName:sub(1, #PartialNameOfPlayerToChange):lower() == PartialNameOfPlayerToChange:lower() then
PlayerToChange = p.Character
break
end
end
if PartialNameOfPlayerToChange:lower() == "me" then
PlayerToChange = Player.Character
end
if PlayerToChange then
local Reason = Message:split(PartialNameOfPlayerToChange)[2]
local WarningGUI = PlayerToChange.Rank.Main.Special
WarningGUI.Text = Reason
end
end
end)
end)
end
end
So this would work with display names, yes?
Yes both usernames/displaynames
It works! Thank you so much! (charactersssss)
Although you’ve already marked a solution, I should say anyway that regardless of your use case, you should still strive to make it only work with usernames and not display names. Display names are purely for the sake of display, they aren’t to be used for anything else in any systems.
Using display names for more than just display is going to present you with some hard-to-solve string collision problems, especially when two users sharing the same display name are in the server or a display name is similar to a username. This issue I already see in the code presented as the solution.
Unless you want to take the time to manually resolve this conflict by also adding in a selector if more than one match is found rather than making the assumption that the command will always get the right person, you’ll probably find yourself back in this category highlighting this exact problem.
You can still use usernames though, although yes there may be conflict, there is still the option to use usernames, and there isnt very often two people with the same display name, especially in a small game like mine