How would I make a chat argument?

Hey, I want to know how I’d make a chat argument kinda thing.

Basically I want to detect what the message is after the players name is said. I believe string.sub() is an option but I simply don’t know how to use it very efficiently, and the wiki didn’t make much sense either.

My code:

 local function kick(target, message)
    target:Kick('You have been kicked. Reason: ' .. message)
 end
game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
	    if msg:sub(1,6):lower() == '!kick ' then
		    kick(game.Players[msg:sub(7)], IDontKnowHowToSeeWhatsAfterTheName)
	    end
    end)
end)

Thanks! :slight_smile:

That wouldnt work, as I’d have to type the players name, and I have no way of seeing (from my current knowledge) when their name ends, to then send the message.

Basically, after I find the end of the players name + a space, I’d like the “IDontKnowHowToSeeWhatsAfterTheName” piece of code, to be the message.

I’ve tried to figure out a solution in studio, and I found a problem: It is taking the entire argument after the !kick even if it has spaces. To make this work, you’d need to figure out the length of the player’s name, then cut off the rest of the message.

How would I do that?

msg:sub(7):sub(#msg:sub(7):match(’^[%a%p%d]*’)+2, #msg)

3 Likes

Figured it out!

local playerKicked = msg:sub(7)
local restofMsg = msg:sub(playerKicked:len() + 7)

Then replace “IDontKnowHowToSeeWhatsAfterTheName” with “restofMsg”

This, unfortunately didn’t work, as the error said “wayIxn Test” is not a valid member of players.

My current code:

local function kick(target, message)
    target:Kick('You have been kicked. Reason: ' .. message)
end

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
	    if msg:sub(1,6):lower() == '!kick ' then
		    local playerKicked = msg:sub(7)
		    local restofMsg = msg:sub(playerKicked:len() + 7)
		    kick(game.Players[playerKicked], restofMsg)
	    end
    end)
end)

Ah, I see the error. It’s still taking the rest of the msg. Will look for an alternative solution.

1 Like

In order to do this, I recommend that you break up
the string into all of the different words after you have confirmed the string is a command. You can do this as shown:

local Prefix = ";"

Player.Chatted:Connect(function(Message)
    local IsCommand = string.match(Message,"^"..Prefix)
    if IsCommand then
        Message = string.gsub(Message,IsCommand,"",1) --Removes the prefix
    else
        --Not a command
    end
end)

Once you have determined that your message is a command, you can split it into different words (This goes inside the IsCommand if statement):

local Arguments = {}

for Argument in string.gmatch(Message,"[^%s]+") do
    table.insert(Arguments,Argument)
end

Now, to answer your question, you can get each individual word by doing something like this:

local Word = Arguments[2] --Would get the second word in the original string

Hope this helps!

(There is more information here: How to make basic admin commands)

6 Likes

Thanks! I’ll be trying to maneuver my way around this and attempt to get it working! If I don’t I’ll probably ask a question, if you don’t mind about what I’m having troubles with. :slight_smile:

3 Likes

There is no reason that is shouldn’t work because I think I’ve tried it myself a while ago. I should also mention that the number used to index Arguments relates to the original word order.

2 Likes