How would I successfully split this table and convert it to a string?

I’m upgrading my warns command by adding a billboard gui which displays this message:

“YOU HAVE BEEN WARNED FOR: (–Third argument of Argument Table–)”

However, im having a hard time splitting the table, and concentating it with the Text label’s text.

For more clarity:

Lets say I typed in:

J:Warn jeff Abusing Chat Commands

The Break String function would convert the sentence to:

jeff
Abusing
Chat 
Commands

before storing these words in an array.

What i want to achieve to have the text label say is:

“You Have Been Warned For: Abusing Chat Commands”

I’m not sure how to split the string to only include a table with those 3 words, before converting that table into an string, which I could then concentate with my “You Have Been Warned For:” string.

The code I’m using:

elseif Command == "J:Warn" then
				print("Warning...")
				local Target = ArgsTable[2]
				if string.lower(Target) == "all" then	

					for indexPlayer, Player in pairs(game.Players:GetPlayers()) do
						if Player == plr then
							print("Will not warn, as target is admin")
						else
							local Head = Player.Character:WaitForChild("Head")
							if Head then
								local WarningGuiClone = WarningBillboardGui:Clone()
								WarningGuiClone.Text.Text = "You Have Been Warned For: "..string.split(table.concat(stringMsg, ArgsTable[3][2], " "))
								WarnEvent:FireClient(Player, ArgsTable[3])
								Player:WaitForChild("Warns").Value += 1
								if Player:WaitForChild("Warns").Value == MainModule.MaxWarns then
									Player:Kick("You Have Been Warned Too Many Times")
								end
								delay(10, function()
									WarningGuiClone:Destroy()
								end)
							end

						end
					end
				else
					local Found_Player = MainModule.FindClosestMatchString("PlayerSearch", game.Players:GetPlayers(), Target)
					if Found_Player ~= nil then
						local Head = Found_Player.Character:WaitForChild("Head")
						if Head then
							local WarningGuiClone = WarningBillboardGui:Clone()
							WarningGuiClone.Text.Text = "You Have Been Warned For: "..string.split(table.concat(stringMsg, ArgsTable[3][2], " "))
							WarningGuiClone.Parent = Head
							WarnEvent:FireClient(Found_Player)
							Found_Player:WaitForChild("Warns").Value += 1
							if Found_Player:WaitForChild("Warns").Value == MainModule.MaxWarns then
								Found_Player:Kick("You Have Been Warned Too Many Times")
							end
							delay(10, function()
								WarningGuiClone:Destroy()
							end)

						end

					end
				end

Sorry if this confuses you :grinning:
P.S.
Error I’m currently getting:

  20:30:13.322 - ServerScriptService.ChatHandler:135: invalid argument #1 to 'concat' (table expected, got string)

To do this, you would need to set a variable of the original table (ArgsTable) without the first argument. Then, you will concat that table. It will look something like this:

local newTable = table.remove(ArgsTable,1)
local joined = table.concat(newTable, " ") -- This is a string value of the warning reason

And then I could join that joined variable to any other string? Like:

Text = "Hey there" ..joined

Do you already have the above Break String function working? If so, here’s code I wrote up:

local yourString = {
    "jeff",
    "Abusing",
    "Chat",
    "Commands"
}

local newString = "You Have Been Warned For: " .. yourString[2] .. " " .. yourString[3] .. " " .. yourString[4]
print(newString)

Now, the problem with the above code I wrote is that it’s assuming the same message over and over again. If, at some point, you decide to have different messages that may have more or less words in it, this will be a problem.

To somewhat fix that problem, here’s new code:

local yourString = {
    "jeff",
    "Abusing",
    "Chat",
    "Commands",
}

local newString = "You Have Been Warned For: "

for i = 2, #yourString, 1 do
    newString = newString .. yourString[i] .. " "
end

print(newString)

This new code should work well as long as any other warning messages follow the same pattern; that is, with the first element of the table being the player’s name, and with the rest to be placed into the warning message for the player.

Feel free to let me know if they’re any questions or concerns.

2 Likes