How to insert the contents of a table within a string message

Hello All,

I’m trying to insert the contents of a table into the middle of a pre-made string, but I don’t know how. Part of the difficulty is the number and names of items within the table will change during game play. Here’s the setup:

In my game a player can buy an item by pressing a button. To successfully buy the item the player needs, not only money, but various resources as well. With Scripts, the game checks to make sure the player has enough resources and money. If the player doesn’t have enough money or resources, that money/resource name is added to a table within a Script. I plan to have a message screen pop up stating that the player doesn’t have enough resources/money, but I want the message to state which items that are lacking. My thought was to type out a string message and insert the contents of the table of lacking resources within the string.

My issue is how to insert the table contents within the string of the message.

Example of string message: “Purchase Incomplete. Not enough " … tableContents … " in your inventory.”

So that the message looks like: “Purchase Incomplete. Not enough Money, Steel, Aluminum in your inventory.”

The number of items in the table can vary depending on how many resources a player might be lacking at any given moment when they click on the buy button.

I’m newer to understanding tables and don’t know too many strategies for table manipulation. Any suggestions for how to insert the table contents within the string message?

you could do

tostring(table.unpack(TABLE)) -- replace "TABLE" with the table

for commas:

table.concat(TABLE, ", ") -- replace "TABLE" with the table
3 Likes

Ooo… that’s very interesting, and simple. I’ll try it out. Thanks!

table.concat returns a string so you don’t need to use tostring.

1 Like

I tried both methods, and both result in the same error:

attempt to index string with ‘Text’

Any clarification on why this is happening or how to fix it? The actual lines of code I tried are:

messageText.Text = "Purchase Incomplete. Not enough " .. tostring(table.unpack(compound)) .. " in your inventory."

messageText.Text = "Purchase Incomplete. Not enough " .. table.concat(compound) .. " in your inventory."

messageText.Text = "Purchase Incomplete. Not enough " .. table.concat(compound, ", ") .. " in your inventory."

Using your suggestion gave the error:

attempt to index string with ‘Text’

I think you should do this first and then tell us what the output is:

local Output = table.concat(compound, ", ")
print(Output)
print(type(Output))
1 Like

The two print lines yielded:

07:06:05.134 AluminumIngot, SteelIngot, CopperIngot - Client - WeaponsScript:55
07:06:05.134 string - Client - WeaponsScript:56

It seems like the variable messageText should be a textlabel but it’s a string.

That means the the messageText variable either doesn’t exist/has no value (nil) or it’s a string

Good call. I had mixed up what messageText was referencing. I needed to remove the .Text and the previous suggestions worked perfectly. Thanks all!

Hey, this topic is probably dead, but how could I loop through an table and print them all as strings one at a time?

You would use a for loop:

for key, value in pairs(table) do
   --"table" is the table associated with it
   print(key, value) --prints the key and the value associated with it
end

If you want them to be combined in a single value, you would do:

local data = ""

for key, value in pairs(table) do
   data ..= key .. value .. "\n" 
   --concatenates the "data" variable with the key, value, and a line break
end

print(data)