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

Why isn't my string printing properly with using string.format?

Hi!

I'm attempting to format a string using the text [[ ]] thing. I have come across unexpected behavior that I don't know how to solve.

local a = [[Potatos = %s
Carrots = %s
]] -- This is a multi-line text.

string.format(a, "YES", "NO") -- Format the a string with string.

print(a)

-- Result:
-- Potatos = %s
--Carrots = %s

--Expected result:
-- Potatos = YES
-- Carrots = NO

How would I get this to print like this? I've looked at so many wiki articles and nothing seems to be working.

Thanks, Nathan.

1 answer

✓ Accepted answer

Your problem is that you aren't updating the 'a' variable. String functions will not update the environment for you, you have to redefine it yourself.

local a = [[Potatos = %s
Carrots = %s
]]

a = string.format(a,"YES","NO")
print(a)

This can be shortened as well.

print(tostring([[Potatoes = %s
Carrots = %s]]):format("YES","NO"))

2 comments

1

(Minor) To be even shorter, you can take out the word 'tostring' (though you still need the parentheses). It should end with a newline to be consistent with the string block, though

0

Fantastic answer! Thank you very much, can't believe it was that simple.

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