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

Is it possible to convert a string into a table?

Okay, so I have a custom chat and when the user chats something it sends this message to the chat area. Everything works fine and how I want it to work for now but my next step is creating some sort of way to filter out rude words from even being sent. I already have spam prevention kinda but my main focus is stopping inappropriate words.

Now when I first thought to my self, how am I gonna do this bman the first idea that came to my mind is some how saving every word in the String into a table. My issue is, how would I seperate a string value into a table value when separating the spaces. Now I'll go in depth to what I mean if you are still confused, basically when the user chats something I want it to check this message before sending it. It will go through every word in this sentence a.k.a String. I want it to somehow save this String to a table word by word, so it would remove the spaces and only save the words. For example, a string value like this: "Hello my name is Bman" would then be saved to the table like this {"Hello", "my", "name", "is", "Bman"}. This way I can create a loop that scans through all the table values and if any of the table values equals one of the inappropriate words I will have defined somewhere else it will prevent the user from sending the message.

So to sum that all up, I need some sort of information about how to convert a String like this: "Hello my name is Bman" to this: local table = {"Hello", "my", "name", "is", "Bman"}

If you have any questions just ask. Wiki links ARE useful, if you found a link to somewhere describing it I would really love this, I searched all over the internet but I either missed it or could not find exactly what I was looking for!

Here is the code, it is a localscript found in a ScreenGui inside StarterGui:

repeat wait() until game.Players.LocalPlayer
wait(1)
local StarterGui = game:GetService('StarterGui')
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
---------------------------------------------------------------------------
--Variables
---------------------------------------------------------------------------
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local lastMessage
local chatBar = script.Parent.ChatBar
local cantChat = false
_G.moving = false
---------------------------------------------------------------------------
--Link to custom events
---------------------------------------------------------------------------
local playerChatted = game.ReplicatedStorage.playerChatted
---------------------------------------------------------------------------
--Remote Functions
---------------------------------------------------------------------------
local sendChatMessage = game.ReplicatedStorage.sendChatMessage
---------------------------------------------------------------------------
--Functions
---------------------------------------------------------------------------
function createCountDown(timeamt)
	repeat wait(1)
		timeamt = timeamt-1
	until timeamt == 0
end

function spamAlert()
	local normalcolor = chatBar.BackgroundColor3
	local brickcolor = BrickColor.new("Really red") --Create our new BrickColor
	local color = brickcolor.Color
	chatBar.BackgroundColor3 = color
	chatBar.Text = "Please don't repeat the same message!"
	cantChat = true
	local countdown = createCountDown(3)
	cantChat = false
	chatBar.BackgroundColor3 = normalcolor
	chatBar.Text = "Type / to chat or click this bar!"
end

function safeChatCheck(msg)
	local messageSafe
	if (lastMessage ~= nil) and (msg == lastMessage) then
		spamAlert()
		messageSafe = false
	else
		messageSafe = true
	end
	
	return messageSafe
end

function createChatLine()
	local textlabel = Instance.new("TextLabel")
	textlabel.BackgroundTransparency = 1
	textlabel.TextXAlignment = "Left"
	textlabel.Size = UDim2.new(1,0,.1,0)
	textlabel.Position = UDim2.new(0,0,0.76,0)
	textlabel.Name = "chatbar"
	return textlabel
end
---------------------------------------------------------------------------
--Global Functions
---------------------------------------------------------------------------
function _G.addMessage(player, chat)
	if (_G.moving == true) then
		repeat wait() until _G.moving == false
	end
	_G.moving = true
	print(_G.moving)
	for _, chatbars in pairs (script.Parent.chatFrame:GetChildren()) do
		if (chatbars.ClassName == "TextLabel") then
			chatbars:TweenPosition(UDim2.new(0,0,chatbars.Position.Y.Scale-.1,0), "Out", "Quad", .5, true)
			wait()		
		end
	end
	local chatbar = createChatLine()
	chatbar.Parent = script.Parent.chatFrame
	chatbar.Text = player.Name .. ": " .. chat
	_G.moving = false
end

---------------------------------------------------------------------------
--Events
---------------------------------------------------------------------------
Mouse.KeyDown:connect(function(Key)
	if (Key == string.char(13)) then
		if (cantChat == true) then
			return
		end
		if (script.Parent.ChatBar.Text == "Type / to chat or click this bar!") or (script.Parent.ChatBar.Text == "Please don't repeat the same message!") then
			return;
		end
		local checkMsg = safeChatCheck(script.Parent.ChatBar.Text)
		if (checkMsg == true) then
			sendChatMessage:InvokeServer(script.Parent.ChatBar.Text)
			script.Parent.ChatBar.Text = "Type / to chat or click this bar!"
		end
	end
end)

Mouse.KeyDown:connect(function(Key)
	if (cantChat == true) then
		return
	end
	if Key:lower() == "/" then
		script.Parent.ChatBar:CaptureFocus()
	end
end)
---------------------------------------------------------------------------
--Custom Events Listener
---------------------------------------------------------------------------
playerChatted.OnClientEvent:connect(function(player, msg)
	_G.addMessage(player, msg)
	lastMessage = msg
end)

1 answer

✓ Accepted answer

So, converting a string to a table using full words?

This will require string patterns. We want to use string.gmatch to detect any full words, the pattern for that would be %w+, which will get any alphanumeric group. A group ends at a whitespace(space), comma, other diget( , ^).

local str = "The fox jumped over the bridge 12 times"

function convertToTable(msg)
	local sortedMsg = {}
	for i in string.gmatch("%w+",msg) do
		table.insert(sortedMsg,i)
	end
	return sortedMsg
end

local tab = convertToTable(str)
--{'The','fox','jumped','over','the','bridge','12',times'}
print(table.concat(tab," "))
--> The fox jumped over the bridge 12 times

4 comments

0

Thank you!

0

Do you know any good links to articles containing all string patterns?

1

nvm I just realized string patterns was a link, sorry :D

0

No problem, PM me if you have any further questions(:

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