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

How would I simplify this so I don't make a thousand local variables?

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?

1 answer

✓ Accepted answer

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

2 comments

0

I take it you meant "end" instead of "edn", but also well said.

0

Thank you!

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