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 5 years ago

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

local str1 = "Hello, "
local str2 = "World!"

print("abc" .. "def") -- Would print "abcdef".
print(str1 .. str2)  -- Would print "Hello, World!".

-- Lua numbers can also be automatically converted to strings. E.g:
local age = 20

print("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:

-- All of these would fail miserably:

-- error:  attempt to concatenate a nil value
print("nil:  " .. nil)

-- error: attempt to concatenate a boolean value
print("boolean:  " .. true)

-- error:  attempt to concatenate a table value
print("table:  " .. {})

-- error: attempt to concatenate a function value
print("function:  " .. function() end)

-- error: attempt to concatenate a thread value
print("coroutine:  " .. coroutine.create(function() end))

 -- error: attempt to concatenate a userdata value
print("Vector3:  " .. Vector3.new())

------------------------------
-- All of these are fine however:

-- Would print "nil:  nil".
print("nil:  " .. tostring(nil))

-- Would print "boolean:  true".
print("boolean:  " .. tostring(true))

-- Would print something like "table:  table: 12054C50".
print("table:  " .. tostring({}))

-- Would print something like "function:  function: 2126CC78".
print("function:  " .. tostring(function() end))

-- Would print something like "coroutine:  thread: 119CE48C".
print("coroutine:  " .. tostring(coroutine.create(function() end)))

-- Would print "Vector3:  0, 0, 0".
print("Vector3:  " .. tostring(Vector3.new()))

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
local amountOfTimes = 10

script.Parent.ResponseDialog =  "You have clicked me "..tostring(amountOfTimes).. "times")