Typing / Typewriter Effect

I’ve been trying to reproduce a typing effect that you find in many older console based RPG’s.  I’ve attempted a few different approaches but can’t seem to get it.  Could anyone offer any advice?  Thanks!

local function loadWords(s) local temp = {} for word in string.gmatch(s, "[^%s]+") do table.insert(temp, word) end return temp end local function updateDialog(dialog, str) dialog.text = dialog.text .. " " .. str end local function displayWords(dialog, words) for i = 1, #words do dialog.text = dialog.text .. " " .. words[i] local step = 250 timer.performWithDelay(2000 + step \* i, updateDialog(dialog, words[i])) end end local myText = display.newText( "", 200, 200, native.systemFont, 22 ) local t = loadWords("Here is some text!") displayWords(myText, t)

My dear friend, you forget about closures!  

local function updateDialog(dialog, str) dialog.text = dialog.text .. " " .. str end

Should be

local function updateDialog(dialog, str) return function() dialog.text = dialog.text .. " " .. str end end

It works now!

Also, this was helpful:

http://www.coronalabs.com/blog/2013/05/29/wednesday-faqs-timers-and-events/

My dear friend, you forget about closures!  

local function updateDialog(dialog, str) dialog.text = dialog.text .. " " .. str end

Should be

local function updateDialog(dialog, str) return function() dialog.text = dialog.text .. " " .. str end end

It works now!

Also, this was helpful:

http://www.coronalabs.com/blog/2013/05/29/wednesday-faqs-timers-and-events/