I’m currently looking into how I can make a command where it would auto fill the user. For example, “!warn avi”. In the example, I only had to type part of my username. Any help would be great, thanks!
try this
for i,plr in pairs(game.Players:GetChildren()) do
if plr.Name:match(usernamepart) then
--do stuff to plr
end
end
Make it for i, plr in pairs(game.Players:GetChildren)) do
, or GetPlayers. You forgot do.
do ps:FindFirstChild(plr.Name)
Typically the approach here is to compare if what you’ve typed matches a player’s username to the extent that you typed. For this you will need both sub and length from the string library. The reason why I don’t recommend match is if the string you want is in any part of the username, it can flag.
local usernames = {"Seventeenth_February", "February_Seventeenth"}
local partialInput = "Feb"
--- For this, we will pretend that we use the command "!warn Feb", expecting that
-- only "February_Seventeenth" will get warned and not "Seventeenth_February".
-- With sub:
for _, username in ipairs(usernames) do
local isThisUser = username:sub(1, #partialInput) == partialInput
print(username, isThisUser)
end
--> Seventeenth_February: false
--> February_Seventeenth: true
-- With match:
for _, username in ipairs(usernames) do
local isThisUser = username:match(partialInput) ~= nil
print(username, isThisUser)
end
--> Seventeenth_February: true
--> February_Seventeenth: true
Notice how in the case of sub, if I type “Feb” it will know that I’m trying to act on “February_Seventeenth” and not “Seventeenth_February”, however in the case of match both usernames will get checked. This is because both usernames have the match “Feb”. If a GetPlayers table was sorted this way, “Seventeenth_February” would get acted on by the admin command and that’s not the intended target of the command. You can try a thing or two with patterns instead but ultimately there’s no need to go through that effort when you can make a simple sub comparison.
User types a partial name → Go through a list of users → Get the length of the typed part and take a substring of that length from whoever is currently being checked → Compare if that substring and the typed part are the same → If yes, this is the user you’re looking for
You can then act on that user as you like. If you’re using GetPlayers then you don’t need FindFirstChild again because you already have the player accessible as a variable from the loop.
for _, player in ipairs(Players:GetPlayers()) do
if sub_matches_typed then
warn(player)
end
end
what would I replace this with? sub_matches_typed
It’s all example code. The details are mainly in the post: sub_matches_typed is a reference to the code sample above it explaining why you should use substrings over matches. You can use that code under the “with sub” comment as a reference for what’d go here.
Switch that out with string.lower():
for i, Player in ipairs(Players:GetPlayers()) do
if string.lower(Player.Name):match(string.lower(PlayerNameInCommandMessage)) then
PlayerToWarn = Player
end
end
Would PlayerToWarn be a variable? local playertowarn
local players = game:GetService("Players")
local function kickPlayerByNickname(nick)
local playersFound = {}
for _, player in ipairs(players:GetPlayers()) do
if player.Name:lower():match("^"..nick:lower()) then
table.insert(playersFound, player)
end
end
if #playersFound == 1 then
local player = playersFound[1]
player:Kick("You have been kicked!")
end
end
Yes, you can name it whatever you want.
I made a better version, here:
local function findPlayerByName(Name)
local PlayerCount = 0
local PlayerFound
for i, Player in ipairs(Players:GetPlayers()) do
if string.lower(Player.Name):match(string.lower(Name)) then
PlayerCount += 1
if PlayerCount > 1 then
PlayerCount = 0
PlayerFound = nil
break
else
PlayerFound = Player
end
end
end
return PlayerFound
end
print(findPlayerByName(PlayerName))
(just use findPlayerByName inside your function)
You can anchor the search pattern to the start of the subject string via the caret key if you’re using “:match()”, as I have done in the example I provided.
This isn’t ranking and isn’t showing any errors. Do you want me to send you the full script in DMs?
It’s not a completed script, it’s just an example for you to take inspiration from.
You’d need to hook it to each player’s “.Chatted” event etc.
Mhm, I did. I added your script into my own script. I think I did it wrong.
Can you send me your script? I can help to fix it.
I just sent the script to you! Thanks for helping!