Players:GetUserIdFromNameAsync() failed: Unknown user

image

I am getting an error: " Players:GetUserIdFromNameAsync() failed: Unknown user " – Line 18.

Please Debug and give a solution.

Script PasteBin

Hmmm. I know this is probably a longer and less effective solution, but instead, could you try defining the player using local banningPlayer = Players:FindFirstChild(Username)
and then SetAsync for banningPlayer.UserId

-- Holders
local Command = "/Ban "
local Command2 = "/Unban "
local Admins = {1531912120}
 
local Players = game:GetService("Players")
 
local BanData = game:GetService("DataStoreService"):GetDataStore("BanStore")
 
-- Functions
 
game.Players.PlayerAdded:Connect(function(Player)
    if table.find(Admins,Player.UserId) then
        Player.Chatted:Connect(function(Message)
            if string.sub(Message, 1, string.len(Command)):lower() == Command:lower() then
                local Username = string.sub(Message,string.len(Command) +1):lower()
                if Username then
                    local UserId = Players:GetUserIdFromNameAsync(Username)
                    print(UserId)

                    if UserId then
                        BanData:SetAsync(UserId.."-BannedUser")
                        Username:Kick("")
                    end
                end
            elseif string.sub(Message, 1, string.len(Command)):lower() == Command2:lower() then
                local Username = string.sub(Message,string.len(Command) +1):lower()
                
                local Success,ErrorMessage = pcall(function()
                    BanData:RemoveAsync(Players:GetUserIdFromNameAsync(Username).."-BannedUser")
                end)
            end
        end)
    end
end)

Try this, I separated a couple of things to prevent the script from erroring a bit less
It could be cause you’re attempting to call 2 functions inside of each other (GetUserIdFromNameAsync & SetAsync)

1 Like

Maybe you attempted to do it in a local server. However, the ID numbers of the players in the local server are ordered in negative numbers.

1 Like