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

How to put variables in strings?

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?

2 answers

✓ Accepted answer

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.

local amountOfTimes = 10

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

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