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

Is there a better way to use if to check same thing?

Is there a better way to use if to check more than one thing....?

Like: I do:

if string == "taco" or string == "pizza" or string == "duck" then

better way:

if string == ('taco'or'duck') then

or something similar?

1 comment

1

You would use a switch/ case select but Lua does not have that functionality. http://lua-users.org/wiki/SwitchStatement

1 answer

✓ Accepted answer

Not really.

There's not really a better way. However, if there are a ton of string you would like to check, you could loop through a table and return if the string matches any of the values stored in the table.

Example,

local strings = {
	"taco";
	"cheese";
	"men";
	"women";
	"happy";
	"sad";
	"pyrondon";
	"is";
	"weird"
}

function check(string)
	for i,v in pairs(strings) do
		if string == v then
			return true
		end
	end
	return false
end

if check("men") then
	print("Returns True")
end

That would only really be practical if you have tons of strings you want to check.

Good Luck!

Warning, there might be a better way xD

1 comment

0

Well dang. Thanks for this though, could probably use it if I have like 20 or more strings. Lol

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