Problem with concatenation

I am busy making a plugin that changes a script, I am not going to go into too much details, so I will simplify it.

This is an example plugin(Not the one im making but it has the same issue):

The plugin will will create a script which runs some code in a module parented to the script.

To be simple the module will have a function like this:

function ExampleModule.DestroyObject(Object)

Here is the issue:

I need to change the source of the code of the script that I am inserting which will look something like this:

Script.Source = "ExampleModule.DestroyObject(" .. Selection:Get()[1] .. ")"

But the problem is that you can’t concatenate an instance with a string.

How can I get around this?

1 Like

You can use tostring for this.

Script.Source = "ExampleModule.DestroyObject(" .. tostring(Selection:Get()[1]) .. ")"

The above code converts the instance into a string of its own name. So it will look somethings like “ExampleModule.DestroyObject(INSTANCE_NAME)”

Instead of using tostring() you can just do Selection:Get()[1].Name since all Instances have a Name property which is a string. Another alternative would be to use string.format to avoid multiple concatenations by doing.

Script.Source = string.format("ExampleModule.DestroyObject(\"%s\")", Selection:Get()[1].Name)

Yeah you’re right. A little tip for string.format is you can use %q which adds “” automatically avoiding confusion with \