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

How to put variables in strings? [closed]

Asked by 8 years ago

I am not exactly sure how to do this. So my code is: script.Parent.ResponseDialog = "You have clicked me times" and in between "me" and "times" I want to put a variable in there, but how do I do so?

Locked by User#19524

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
7
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago
Edited 6 years ago

You use the string concatenation operator ... It basically serves to merge two string values together into one. E.g:

01local str1 = "Hello, "
02local str2 = "World!"
03 
04print("abc" .. "def") -- Would print "abcdef".
05print(str1 .. str2-- Would print "Hello, World!".
06 
07-- Lua numbers can also be automatically converted to strings. E.g:
08local age = 20
09 
10print("I am " .. age .. " years old.") -- Would print "I am 20 years old.".

Note that other Lua types will have to be transformed into strings using the "tostring()" function before being concatenated:

01-- All of these would fail miserably:
02 
03-- error:  attempt to concatenate a nil value
04print("nil:  " .. nil)
05 
06-- error: attempt to concatenate a boolean value
07print("boolean:  " .. true)
08 
09-- error:  attempt to concatenate a table value
10print("table:  " .. {})
11 
12-- error: attempt to concatenate a function value
13print("function:  " .. function() end)
14 
15-- error: attempt to concatenate a thread value
View all 40 lines...

Hope this helps. If it did, please upvote my answer and mark it as accepted. If you need anything else, feel free to send me a message.

Ad
Log in to vote
0
Answered by 8 years ago
1local amountOfTimes = 10
2 
3script.Parent.ResponseDialog =  "You have clicked me "..tostring(amountOfTimes).. "times")