Is there a way to detect spaces in a command? The command I want to make is /pay PlayerName IntegerAmount
I am aware that you can do something like this if there are two parts in a command
function onChatted(message, player) local filteredText = game.Chat:FilterStringForBroadcast(message,player) if filteredText:sub(1,5) == "/pay " then local recipent = filteredText:sub(6) end end game.Players.PlayerAdded:connect(function(player) player.Chatted:connect(function(message) onChatted(message, player) end) end)
But how would I detect how to find the number, due to the length of player names being inconsistent?
You're on the right track. In order to pick apart the player's chat, you need to use string patterns.
String patterns are exactly what they sound like - ways to detect patterns in strings.
You want to detect the recipient, proceeding the "/pay " command. The pattern for this would be .+%s
- i'll break down why:
. stands for any character, since player's names can be most characters.
+ stands for consecutive matches of the preceding pattern.
%s stands for a space.
You can use this patern to detect who you are paying to. Note - it will be inclusive of the space, so you'll have to omit it using string.sub
.
local recipientPattern = ".+%s" --Pattern for recipient of money function onChatted(message, player) local filteredText = game.Chat:FilterStringForBroadcast(message,player) if filteredText:sub(1,5) == "/pay " then local recipient = filteredText:sub(6):match(recipientPattern) recipient = recipient:reverse():sub(1):reverse() --omit whitespace print(recipient); end end
Now that you have the recipient, you can move onto getting the amount. Use the string.len
function, along with the string.sub
function to omit the recipient from the string. Then, use the %d+
string pattern to detect integers in the string.
local recipientPattern = ".+%s" --Pattern for recipient of money local amountPattern = "%d+" --Pattern for amount of money function onChatted(message, player) local filteredText = game.Chat:FilterStringForBroadcast(message,player) if filteredText:sub(1,5) == "/pay " then local recipient = filteredText:sub(6):match(recipientPattern); recipient = recipient:reverse():sub(1):reverse() --omit whitespace print(recipient); local amount = filteredText:sub(recipient:len()+6):match(amountPattern) print(amount); end end