Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a way to detect spaces in a command?

Asked by
gitrog 326 Moderation Voter
7 years ago

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?

0
That's how you do it. e-e Also, you could use set up a function to get the selected player; take Person299 and Kohltastrophe's Admin Commands as references. TheeDeathCaster 2368 — 7y
0
I want to add a number after and I don't know how to distinguish a username from the number gitrog 326 — 7y

1 answer

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

What you need

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.


Getting the recipient

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

Getting the amount

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
Ad

Answer this question