How do I check if text in a text box is in a list of words?

I was trying to create a game sort of like what’s the word, where the player would have to type a word under several categories (ex. starts with S, is 4 letters long, originates from Latin), but I couldn’t figure out how to check if the text box has a specific word. Here is the code:

Script in ServerStorage:

local rs = game:GetService("ReplicatedStorage")
local plrs = game:GetService("Players")
local letter = rs.weDoingLetter
local plr = plrs.LocalPlayer
local plrui = plr:WaitForChild("PlayerUI")
local tb = plrui.Game.TextBox
local letterchosen = math.random(1,26)

letter.onServerEvent:Connect(function()
	if letterchosen == 1 then
		if tb.Text == "A" or "Ab" or "As" or "etc" then
			plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 30
		end
	end
end)

LocalScript in TextBox:

local rs = game:GetService("ReplicatedStorage")

script.Parent.FocusLost:Connect(function()
	rs.weDoingLetter:FireServer()
end)

Thank you in advance.

Well, I guess this depends sincerely on how complex you intend to make the answers.

If we’re going off of your topic title it is very simple utilizing dictionaries and checking them for an index when the time arrises:

local wordBank = {
["Hello"] = true;
["Goodbye"] = true;
}

letter.OnServerEvent:Connect(function(player,text)
	if text and text ~= "" then
		if wordBank[text] then
			-- word found
		end
	end
end)

-- LOCAL SCRIPT --
local rs = game:GetService("ReplicatedStorage")

script.Parent.FocusLost:Connect(function()
	rs.weDoingLetter:FireServer(script.Parent.Text)
end)

However this doesn’t seem to be the goal given the way your coding is set up.

If you want to randomly select a letter and be able to then identify that letter and the possibilities of correct entries corresponding to that letter, we need to set up more in-depth tables containing the data.

local letterBank = {
{"A","Ab","As","etc"};
{"B","Bb","Bs","etc"}; -- Continue this for the rest of the letters.
}

Now we can use this setup and implement elements from your pre-existing system.

local letterBank = {
{"A","Ab","As","etc"};
{"B","Bb","Bs","etc"};
}

local chosenLetter = math.random(1,#letterBank)

game.ReplicatedStorage.weDoingLetter.OnServerEvent:Connect(function(player,text)
	if table.find(letterBank[chosenLetter],text) then
		-- Passes the requirement
	end
end)

-- LOCAL SCRIPT --
local rs = game:GetService("ReplicatedStorage")

script.Parent.FocusLost:Connect(function()
	rs.weDoingLetter:FireServer(script.Parent.Text)
end)

I would also like to point out some general flaws in your code:

  • You claim your script is in ServerStorage, meaning it will never run in the first place. If this is the case, move it into ServerScriptService so that it will run.
  • You attempt to identify LocalPlayer in a server side script which does not exist, therefore your “plr” argument will always be nil. You can get the player as the first parameter passed in a RemoteEvent.OnServerEvent function.
  • Text changed in TextBoxes do not cross the client to server boundary, you’ll need to pass the text as one of your arguments in the RemoteEvent:FireServer call.

Not sure how much this helps your original question as you were a bit vague in what you truthfully wanted to accomplish, but hopefully this helps you slightly.

A few quick questions: are the answers case sensitive?

How can I display the requirements on a TextLabel?

That’s all. Thanks!