Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to check if message matches string in table?

Asked by 4 years ago

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)
0
Try not to use the same variable for the arguments that were passed. So change the variable msg, different from the argument msg. killerbrenden 1537 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You can use string.find (https://developer.roblox.com/en-us/api-reference/lua-docs/string) and plug in the wanted keyword and the given string.

Furthermore, use a for-each loop to go through every element of the list.

For example:

for i = 1, #list do
    if string.find(msg, list[i]) ~= nil then
        print("found " .. list[i])
    end
end
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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;
Log in to vote
0
Answered by 4 years ago

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

Answer this question