String concatenation help

Essentially, I need help with some string concatenation. I am doing something along the lines of this:

function CreateTooltip(args)
  TextLabel.MouseEnter:Connect(function()
    TextLabel.Text = table.concat(args)
  end)
end

The issue is that if I were to change something in args, the text would not update. Is there anyway around this?

Help is appreciated.

Just set the variable str to the value everytime you want to change it.

The problem is that in order, the a[1] is 12. Maybe make it so that if “a” changes, the string will update.

See I don’t get what either of you are trying to do. If you want something to change just go re-assign the variable.

you can just concatenate your table with the new value

TextLabel.Text = table.concat(a)

That’s the issue though. How would I do that?

The only issue is that I’m having these values passed in via. function, meaning that they would stay the same.

Are you passing table “a” in your function and changing the text within said function?

I’m kinda doing something like this:

function PassIn(stringArr)
    TextLabel.MouseEnter:Connect(function()
        TextLabel.Text = table.concat(stringArr)
    end)
end

The issue is that when the value that the array is referencing is changed, the text won’t update.

Maybe have a function that concats the string with the number. Example (sorry if it’s terribly formatted, I’m on a phone RN):

local function GetText()
    local Result = "The value of a is: " .. a[1]
    return Result
end

TextLabel.Text = GetText()

This way, whenever you set the text, it will give the latest value of a[1].

But then it isn’t dynamic. I plan on using this function many times for different variables, creating a new function each time would make the code very messy.

Then you can modify it to get the table and index like so:

local function GetText(tble, index)
local Result = "The value of a is: " … tble[index]
return Result
end

TextLabel.Text = GetText()

Well… That depends. I mean there is definitely a way around this, but it really depends on how you want to set this up.

The easiest, and technically what all other solutions are based on is simply changing scope. Instead of

function CreateTooltip(args)
  TextLabel.MouseEnter:Connect(function()
    TextLabel.Text = table.concat(args)
  end)
end

Do this

args = {}
function CreateTooltip()
  TextLabel.MouseEnter:Connect(function()
    TextLabel.Text = table.concat(args)
  end)
end

Now args in that inner string is referencing a shared variable since it has a larger scope. So when you change args, the data that the MouseEnter function uses will change.

1 Like

Thanks for the help! I’ll probably just end up periodically setting the table to the module script that has all the values I need, and call those in the concat.

2 Likes