function ToucanBeak(source, find, replace, wholeword) if wholeword then find = '%f[%a]'..find..'%f[%A]' end return (source:gsub(find,replace)) end function WaveBeak(message,player) local newmessage1 = string.lower(message) local newmessage2 = ToucanBeak(newmessage1, "teemo", "warbler", false) local newmessage3 = ToucanBeak(newmessage2, "teemoing", "warbler maining", false) end
Basically ToucanBeak just subs the target word out for something out.
WaveBeak sets everything to lowercase, and then uses ToucanBeak to get the new message.
I intend to do a lot more with this - is there any way I can make it more efficient/use way less local variables?
Use a table. When you find yourself numbering variables, it means you just want a list.
You could use a list:
local simple = { {from = "teemo", to = "warbler"}, } local whole = { {from = "foo", to = "bar"}, } for _, rule in pairs(simple) do message = ToucanBeak(message, rule.from, rule.to, false) end for _, rule in pairs(whole) do message = ToucanBeak(message, rule.from, rule.to, true) end
Also, by the nature of "find/replace" you could just use a dictionary:
local simple = { teemo = "warbler", } local whole = { foo = "bar", } for from, to in pairs(simple) do message = ToucanBeak(message, rule.from, rule.to, false) end for from, to in pairs(whole) do message = ToucanBeak(message, rule.from, rule.to, true) end