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

How to reform strings inside "" into a table?

Asked by
yx10055 57
3 years ago

I know this title may sound bad, but I had no idea how to phrase it. Anyways, how would I turn this:

"Welcome" to "foo" hello "bar" community "foo bar"

Into this:

{"Welcome", "foo", "bar", "foo bar"}

I've tried looking at the Lua Manual and DevForum but I couldn't find anything.

0
Are you trying insert strings into a table? CMVProduction 25 — 3y
0
Nononono. I am trying to make it so when players type quotes in chat, it puts everything in between the quotes into a table yx10055 57 — 3y
0
Nononono. I'm trying to make it so if users say something with quotes it puts everything inbetween the quotes into a table yx10055 57 — 3y

2 answers

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
3 years ago

Rinpix's answer only works if the whole message is in quotes but it doesn't insert the particular quotes into a table.

Here's a proper solution:

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        local tbl = {}
        for i in message:gmatch('%b""') do --balanced strings pattern
            table.insert(tbl, i)
        end
        --now you can do whatever with the table "tbl" and the strings in it
    end)
end)

Lua patterns guide

Ad
Log in to vote
-1
Answered by
Rinpix 639 Moderation Voter
3 years ago

If you want to insert chat messages into a table if they begin and end with quotes, do this in a server script inside of ServerScriptService:

local messages = {}

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        if (#message < 3) then return end

        if ((message:sub(1, 1) == "\"") and (message:sub(#message, #message) == "\"")) then
            table.insert(messages, message:sub(2, #message - 1))
        end
    end)
end)

Now, I wouldn't just throw a bunch of code at you without actually explaining what's being done.

We set up a table that'll hold all of the messages we want, and assign it to a variable called "messages."

Listen for whenever a player joins, and for whenever they chat. Whenever they chat, make sure the message they're sending is at least 3 characters in length, because there needs to be at least 2 quotes, and a character in between them. If the length of the string is less than 3, exit out of the function. Otherwise, check to see if the first character in the message is a quote, as well as if the last character in the message is a quote. If it is, then insert the message into the table, excluding the quotes.

Answer this question