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

How to check if message matches string in table?

I'm trying to make a script so when i type "-get" followed by a string in the table (for example "-get blue") it would print "worked" like it is supposed to, how would i make it check if the argument matches a string in the table?

list = {"yellow", "blue"}
game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local msg = msg:lower()
		if string.sub(msg,1,5) == "-get" and string.sub(msg,6) == list
			then print('worked') else print('didnt work')
		end
	end)
end)

1 comment

0

Try not to use the same variable for the arguments that were passed. So change the variable msg, different from the argument msg.

3 answers

You can use table.find or a for loop. I used table.find because it's simply finding a value that matches the one we have. Example:

local string_1='Wow potato';
local fav_foods={'potato';'pizza';};

string_1=string_1:sub(5);

if table.find(fav_foods,string_1)then
	print'yum'-->yum
end;

You can use the other answer but there is still one problem you said string.sub(msg, 1, 5) == "-get" you forgot to put a space after -get

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