Abbreviating Username for All Players

I am making an admin panel, and currently, my code only works to abbreviate the username of players currently in the server. I want to change the second line, game.Players:GetChildren(), to something else to have it check for all players on the platform, so that if someone attempts to leave the server to evade a ban, they are still able to be banned. I’m not exactly sure how I would go about this, so any assistance would be appreciated.

function getPlayer()
    for i, v in next, game.Players:GetChildren() do
        if string.len(v.Name) >= string.len(username.TextBox.Text) then
            if
                string.lower(username.TextBox.Text) ==
                    string.lower(string.sub(v.Name, 0, string.len(username.TextBox.Text)))
             then
                if v == nil then
                    return false
                end

                return v.Name
            end
        end
    end
end

Well the implementation of this would require you to have a table of all players, essentially “trapping” them since they won’t be GC’d until you free them. And not a table returned by :GetPlayers (notice im not using Children), and checking if the names match one of the player’s names in the table for you to free them later on. You should be really careful and at this point for your use case you might as well just ban by UserId because of the chance you might ban someone else if they share a piece in their name.

I can’t even imagine it because of how complex it would be.

Yeah. I already have an admin panel, and people were complaining about having to type full usernames in, and I stumbled across this RGT video, which is where I got the idea from.

If you skip to 2:02, you see that the player in particular has left the server, however, the suggestion for the player still pops up.

“EXPLOITERS AND ABUSERS? | Roblox’s Got Talent Admin Series #1

(I’m not sure if I can post youtube links, so I provided the name of the video instead)

I recommend making a table and putting the usernames of everyone who joins the game into this table. Then, you can reference this table instead of the players who are still in game.

See game.Players.PlayerAdded.

I never thought of that. Thanks for the suggestion. I’ll try that out.

Also, make sure this table is put on the server and not the client so that if a mod joins after the player who broke the rules they can still be banned with the autocomplete system you have.

See this DevHub article on how you can use a RemoteFunction in order to get the table to the client from the server (assuming the getPlayer() function is on the client)

I used a remote function to get the information, however, it doesn’t seem to be working. It is returning the word table, and then random numbers and letters, such as “[table: 0xcec4bfd0bf7e84fb”.

Server-Side Script

local playersjoined = {}

game.Players.PlayerAdded:Connect(function(plr)
	table.insert(playersjoined, 1, plr.Name)
end)

game.ReplicatedStorage.CheckTable.OnServerInvoke = function()
	return playersjoined
end

Client-Side Script

function getPlayer()
    for i, v in next, game.ReplicatedStorage.CheckTable:InvokeServer() do
        if string.len(v.Name) >= string.len(username.TextBox.Text) then
            if
                string.lower(username.TextBox.Text) ==
                    string.lower(string.sub(v.Name, 0, string.len(username.TextBox.Text)))
             then
                if v == nil then
                    return false
                end

                return v.Name
            end
        end
    end
end
1 Like

If you are seeing the word table with a lot of letters and numbers, it is because you are printing the table itself and not the tables contents.

You can use table.concat(yourTable, ' ') to get your tables contents as a string, or if you have a dictionary use HttpService:JSONEncode()

Anyways, the only issue I see with your code is that you are still referencing v.Name as if it is a player Instance, but instead you should be referencing v because the table contains only usernames.

So, for example, use string.len(v) instead of string.len(v.Name).

I changed everything to v instead of v.Name, and it still isn’t working for some reason. I attempted to print the contents of the table with table.concat, which worked, however, my username is not showing up in the table.

Server-Side

local playersjoined = {"test"}

game.Players.PlayerAdded:Connect(function(plr)

table.insert(playersjoined, 1, plr.Name)

end)

game.ReplicatedStorage.CheckTable.OnServerInvoke = function()

return playersjoined

end

wait(5)

print(table.concat(playersjoined, ' '))

Client-Side

function getPlayer()
    for i, v in next, game.ReplicatedStorage.CheckTable:InvokeServer() do
        if string.len(v) >= string.len(username.TextBox.Text) then
            if
                string.lower(username.TextBox.Text) ==
                    string.lower(string.sub(v, 0, string.len(username.TextBox.Text)))
             then
                if v == nil then
                    return false
                end

                return v
            end
        end
    end
end
function getPlayer() 
     local players = game.ReplicatedStorage.CheckTable:InvokeServer()
     for i, v in next, players do 
          if string.lower(username.TextBox.Text) == string.lower(string.sub(v, 1, string.len(username.TextBox.Text))) then
               return v 
          end 
     end 
end

Please try this code. I fixed your string.sub because in Lua most things begin in 1 where other languages would begin at 0.

Also, I removed your if statement because it prevented the code from working when an entire username was typed.

Would it still return nil if there was no match for the player?

Nevermind. Tested it and it still worked and returned nil if it doesn’t exist. It still is saying that my username is not in the table, and I’m not sure why.

(clipped previous response because it was unneeded)

What happens when you try to print the player name in the PlayerAdded event? Does your name show up when you join?

It doesn’t. It just prints a green line image

Is it possible that you are connecting the PlayerAdded event after you have already been added to the game? If there is other code in the server script it may delay your event from connecting and therefore not see you join.

I think so. It’s on the 263rd line of the server script.

Ah! I think this is our problem.

Try putting all of the code you put above into its own script so that the event is connected immediately and therefore won’t miss you join

That fixed the issue and everything is working now. Thanks!

Glad I could help! I would recommend marking this reply as a solution for people having the same problem who find this post later: