Get players name from chat?

Sorry for the confusing title, but basically, I want to run a fun little admin command type thing, where if a player types this, and then a players name, the thing will happen to that player, but I don’t know how to get the name they typed?

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

if Player.Name == "Pooglies" then

Player.Chatted:Connect(function(msg)

if string.lower(msg) == "/balloon" then

end

end)

end

end)
1 Like

To get the name, you’ll need to get the arguments from the message (if it sounds confusing, it’s alright because it isn’t really).

To do this, you’ll need to use some string manipulation. The function we’ll need to use in specific is string.split(). What this does is simply split the string using the given “separator” (meaning the things in between words). For example, if we did string.split on something like “Hey, my name’s Ben!” with the separator of " " then it’ll return {“Hey,”,“my”, “name’s”, “Ben!”}. If you don’t understand my explanation, make sure to check out the string page shown to you in the link.

Now you know about that function, we’ll need to apply it to your code to get the args (words) of the message. This is pretty simple, and’ll only requires 2 things: your string (the message) and the separator (space, aka " "). We have those 2 things though, so we can get our args through a simple variable such as local args = string.split(msg, " "). Understand?

Now, let’s pretend the message was something like /balloon Benified4Life. Before we get to that though, let’s make sure you understand what the code sees. If you split up that message “/balloon Benified4Life” into a table, it’d look something like {“/balloon”, “Benified4Life”}. Knowing this, there are 2 words in this string and table. Since we know Lua indexs starts on 1, the index of Benified4Life must be 2 as it’s the second in that table.

To retrieve that, you’ll need to get it as if it is a table, since it is. You can do this simply by saying args[2]. Now, if we combine everything, we get args[2] as our player, meaning you can do player = args[2].

Now, the code looks something like this.


if Player.Name == "Pooglies" then

Player.Chatted:Connect(function(msg)

local args = string.split(msg, " ")

if string.lower(msg) == "/balloon" and args[2] then -- MAKE SURE THAT THERE IS ACTUALLY A PLAYER!

local target = args[2]

end

end)

end

end)
2 Likes

To keep this simple, can we assume that the player’s name is going to be the rest of the message?

If so, it could be something like this:

local function get_best_target(target_name)
  for _, player in pairs(game.Players:GetPlayers()) do
    if (player.Name:lower():find(target_name) == 1) then
      print('Best fit player was: ', player.Name)
      return player
    end
  end		
end

game.Players.PlayerAdded:Connect(function(Player)
  if Player.Name == "Pooglies" then
    Player.Chatted:Connect(function(msg)
      msg = msg:lower()
      local cmd = '/balloon ' -- Note the space just to make it easier.
      if msg:sub(1, #cmd) == cmd then
        local plr_name = msg:sub(#cmd+1, #msg)
	local target = get_best_target(plr_name)
	if target then
	  print('Do a funny thing to ', target.Name)
	end
      end
    end)
  end
end)

Additionally this will give you the best fit player for the name given. So if you said /balloon poog it would return you… although, that portion of the function could be improved. I just wanted to give you something that works.

1 Like

I will assume that the rest of the message will be the player’s full name

An alternative to string.split is to use string.sub, which returns a part of the string based on the specified arguments.

Usage:

("Hello world"):sub(start, end) 
("Hello World"):sub(6) -- prints "World". Not specifying the en parameter will default to the end of string
if msg:sub(1, 9) == "/balloon " then
    print(msg:sub(10)) -- prints the remainder of the string
end

Because you use lower() to change all characters to lower case, you’d have to iterate through all of the players and check if their “lowered” name is equal to the one given from the command

1 Like

I think you could use Old Person299’s Admin Commands v2 which have commands where you can edit, i think you could use the code to get the player (such as /fly player) of that script. :+1:

rbxassetid://8646985

One way to improve that would be to use a slightly more complicated pattern, which can split on multiple whitespace characters, not just a single space.
That pattern would be "%s+"
%s is the pattern for any whitespace character, the + means that it will search for multiple, if possible.
This will avoid erroring the script if the user accidentally types 2 spaces