Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a way to detect spaces in a command?

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?

2 comments

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.

0

I want to add a number after and I don't know how to distinguish a username from the number

1 answer

✓ Accepted answer

#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

This discussion is preserved as it was. New answers, comments, and votes are closed.