Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I concatenate a variable?

Asked by 5 years ago

How to a concatenate an already defined variable? for example:

local Var = HelloWorld -- not a string however

local word1 = Hello
local word2 = World

exampleFunction(word1..word2)

Or is this impossible?

1
`local var = "hello"` print(var .. " world") User#24403 69 — 5y

1 answer

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago

Hey Marmalados,

You can not concatenate instances. Concatenation is a process only used for strings. You might be wondering how then can you print the instances. Well, let me take the time to explain how it works.


OOP Review

A part is just an object made through a class defined.(This is called OOP, when you orient the program through objects: Object Oriented Programming) When you try to print an object that is created through the class, it tries to convert the instance to a string. In order to do this, it looks for the str method(Python), or basically the method that is supposed to return the string representation of the object.

What Roblox did with this method is just return the name of the object. So, if you try to do something like this:

part = workspace.Part
part2 = workspace.Part2
var1 = tostring(part)
var2 = tostring(part2)

print(var1) -- Prints: Part
print(var2) -- Prints: Part2

They just convert the part to a string, and that string is the name of the object.


Solution

So, as you may see above, you can get their string representation by converting them to strings. After doing so, you can easily concatenate them. Something like this:

part = workspace.Part
part2 = workspace.Part2
var1 = tostring(part)
var2 = tostring(part2)

print(var1..var2) -- Prints: PartPart2

Conclusion

The reason the print method works with instances is that it gets the string representation of the instance, which Roblox has put as the name of the object. You can only concatenate strings. I hope i helped and have a wonderful day.

Thanks,

Best regards,

KingLoneCat

0
Just for clarification, objects and instances are interchangable. KingLoneCat 2642 — 5y
0
I think you might want to fix the formatting in this answer - it's a bit big. fredfishy 833 — 5y
0
I designed it that way... What's big? The font? Yeah, it's supposed to be organized. KingLoneCat 2642 — 5y
Ad

Answer this question