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

How to get a chunk of a string?

Asked by
Vezious 310 Moderation Voter
8 years ago

Attempt 2, first one was closed. so this time I will be more concrete.

lets say someone chatted this:

/w Felicia OMG YOU NOOB!

/w- Option To Send Private Message

Felicia- The Receivers Name

OMG YOU NOOB! - The Message

Event:FireClient(Player, Message)

How to I Get Player and message from the string?

Btw give me a WARNING before closing it.

I've gotten this so far -

game.Players.PlayerAdded:connect(function(Player)
Player.Chatted:connect(function(Message)
if string.sub(Message, 1,2) == "/w"
then

end
end)
end)
0
Your previous question was closed because you showed no effort in searching the solution. Have you tried searching Roblox wiki/forums? Or Scripting Helpers questions? LetThereBeCode 360 — 8y
0
Yes ma'am. Vezious 310 — 8y
0
Who ever answers this gets a Taco. Vezious 310 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

String patterns hold the secret of the universe

You may be looking to do something like this

local Target, Message = msg:match('^/w%s+(%w+)%s+(.*)$')

Let's break that down
^ is the matching anchor for the start of the string.
/w has nothing special about it, it's to match your /w.
%s is the set for white space characters.
+ is the greedy modifier that tells it to match the preceding set 1+ times.
%w is the set for letters and numbers.
() is the capture. You'll want to google Lua pattern captures.
. is the wildcard and matches everything.
* is the modifier that matches 0+ occurrences of a set, as many times as possible.
$ is the anchor for the end of the string.

0
Only Message is returned. And the player name is returned with it. Vezious 310 — 8y
0
'^/w%s-(%w-)%s-(.*)$' should be '^/w%s+(%w-)%s+(.*)$' LetThereBeCode 360 — 8y
0
It shouldn't be. User#6546 35 — 8y
0
YES. LetThereBeCode, It worked! Thank you, LetThereBeCode, and eLunate! :D I'll still accept your answer eLunate but you might want to update it. Vezious 310 — 8y
0
I made the mistake of somewhere along the line thinking that - was for 1+ User#6546 35 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Help

Well you are going the right spot what I would do though is make a table for each space, this way you can catagorize the command to the player to any other groups after that for advanced admin commands like kohls or something liek tht

text = "/w Felicia OMG U NOOB"
 wordlist = {}
    for word in text:gmatch("[^" .." ".. "]+") do -- seperates names by a ,
        table.insert(wordlist, word)
  end

printing out the table namelist thru a iterator would print out

/w Felicia OMG U NOOB

aak

0
Omg this is confusing. I got it but the message is separated. How do I combine them into one? Vezious 310 — 8y

Answer this question