Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I concatenate a variable?

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 comment

1

local var = "hello" print(var .. " world")

1 answer

✓ Accepted answer

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

3 comments

0

Just for clarification, objects and instances are interchangable.

0

I think you might want to fix the formatting in this answer - it's a bit big.

0

I designed it that way... What's big? The font? Yeah, it's supposed to be organized.

This discussion is preserved as it was. New answers, comments, and votes are closed.