I’m trying to make a game (basically where you guess the word) and I don’t know how I am meant to check whether what the user typed is an actual word or not can someone help
That has nothing to do with what the @OP wants. The @OP wants to check whether a string is an actual English word/term or not.
You would probably have to use a really long dictionary/table of all the words in the English language.
OR, you could use HTTP Service to a website that checks if a string is an actual word or not.
Yeah and I’m not using chatted it’s for a UI
Well, there really is no way to do something like that…
Thanks for the link. I have gained more in-depth experience on the matter and can solve the issue.
There are a few APIs that can do the work for you. I will list some below:
https://www.wordsapi.com (limits to 2,500 requests per day, will return a 400 error if something went wrong)
https://dictionaryapi.dev (free, will return a fail title if the word is not found in the database)
Try avoiding using paid providers as Roblox’s dynamic IP can lead to the word provider blocking your request.
local dataModel = game
local http = dataModel:GetService("HttpService")
local players = dataModel:GetService("Players")
local words = {}
local protectedCall = pcall
local url = "https://raw.githubusercontent.com/dwyl/english-words/master/words.txt"
local function sendHttpRequest(uniformResourceLocator)
local success, result = protectedCall(function()
return http:GetAsync(uniformResourceLocator)
end)
if success then
if result then
local data = result:lower():gsub("[^%w%s]", "")
for word in data:gmatch("%a+") do
table.insert(words, word)
end
end
end
end
local function onPlayerAdded(player)
local function onPlayerChatted(message)
if table.find(words, message:lower()) then
print(message.." is a valid word.")
else
print(message.." is not a valid word.")
end
end
player.Chatted:Connect(onPlayerChatted)
end
players.PlayerAdded:Connect(onPlayerAdded)
sendHttpRequest(url)
Added some major changes to the previous implementation. Words are provided via a gui, if the word exists then it is displayed for all users in their guis, if the word doesn’t exist or it was filtered then only the user which attempted the input is informed as such. Text filtering is performed so as not to break Roblox’s terms and conditions. I will attach a model file below, it contains a server script (ServerScriptService), a remote event (ReplicatedStorage) and a screen gui (StarterGui).
--SERVER
local dataModel = game
local http = dataModel:GetService("HttpService")
local text = dataModel:GetService("TextService")
local players = dataModel:GetService("Players")
local replicated = dataModel:GetService("ReplicatedStorage")
local remote = replicated.RemoteEvent
local words = {}
local playersByAccountAge = {}
local protectedCall = pcall
local url = "https://raw.githubusercontent.com/dwyl/english-words/master/words.txt"
local function sendHttpRequest(uniformResourceLocator)
local success, result = protectedCall(function()
return http:GetAsync(uniformResourceLocator)
end)
if success then
if result then
local data = result:lower():gsub("[^%w%s]", "")
for word in data:gmatch("%a+") do
table.insert(words, word)
end
end
else
warn(result)
end
end
local function getOldestPlayer()
return playersByAccountAge[1][1]
end
local function sortTableOrder()
table.sort(playersByAccountAge, function(left, right)
if left[2] > right[2] then
return left
end
end)
end
local function filterResultForBroadcast(filteredWord)
local success, result = protectedCall(function()
return filteredWord:GetNonChatStringForBroadcastAsync()
end)
if success then
if result then
print(result)
return result
end
else
warn(result)
end
end
local function createFilterResult(word)
local oldestPlayer = getOldestPlayer()
local success, result = protectedCall(function()
return text:FilterStringAsync(word, oldestPlayer)
end)
if success then
if result then
return filterResultForBroadcast(result)
end
else
warn(result)
end
end
local function onRemoteEventFired(player, text)
if table.find(words, text:lower()) then
local filteredWordForBroadcast = createFilterResult(text)
if filteredWordForBroadcast:find("#") then
remote:FireClient(player, false)
else
remote:FireAllClients(true, player, filteredWordForBroadcast)
end
else
remote:FireClient(player, false)
end
end
local function onPlayerAdded(player)
local function onPlayerChatted(message)
if table.find(words, message:lower()) then
print("The chatted message was a valid word.")
else
print("The chatted message was not a valid word.")
end
end
player.Chatted:Connect(onPlayerChatted)
table.insert(playersByAccountAge, {player.UserId, player.AccountAge})
sortTableOrder()
end
local function onPlayerRemoving(player)
table.remove(playersByAccountAge, table.find(playersByAccountAge, {player.UserId, player.AccountAge}))
sortTableOrder()
end
players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
remote.OnServerEvent:Connect(onRemoteEventFired)
sendHttpRequest(url)
--LOCAL
local dataModel = game
local replicated = dataModel:GetService("ReplicatedStorage")
local remote = replicated:WaitForChild("RemoteEvent")
local gui = script.Parent
local frame = gui:WaitForChild("Frame")
local textLabel = frame:WaitForChild("TextLabel")
local textBox = frame:WaitForChild("TextBox")
local textButton = frame:WaitForChild("TextButton")
local function onButtonClicked()
if textBox.Text:len() > 0 then
remote:FireServer(textBox.Text)
else
textBox.Text = "Text required!"
task.wait(3)
textLabel.Text = "TextBox"
end
end
local function onRemoteEventFired(bool, player, filteredText)
if bool then
textLabel.Text = player.Name..": "..filteredText
else
textLabel.Text = "Inputted text was censored or was not a valid word."
end
task.wait(3)
textLabel.Text = "TextBox"
end
textButton.MouseButton1Click:Connect(onButtonClicked)
remote.OnClientEvent:Connect(onRemoteEventFired)
repro.rbxm (11.0 KB)
Proof:
https://gyazo.com/866ee3a0f6fae55a896b553c997d1186
https://gyazo.com/3a2f2b296fb28a6174ed07a8ef4d69db