Need Some Help with Tables

This is hard to explain but here I go,

For example, if the word “cheese” was in filterWords1, and the player says cheese, I want it to print a random word from replaceWords1, vice versa if “orange” and “apple” are in filterWords2 then when the player says apple or orange, it will choose a random word from replaceWords2, not replaceWords1. If you have a better way of doing this that would work too, this is just what I thought of.

Example script:

local filterWords = {
	filterWords1 = {"word in here"},
	filterWords2 = {"word in here", "word in here"},
	filterWords3 = {"word in here", "word in here", "word in here"}
}

local replaceWords = {
	replaceWords1 = {"replacement word in here"}, --replacement words for filterWords1
	replaceWords2 = {"replacement word in here", "replacement word in here"}, --replacement words for filterWords2
	replaceWords3 = {"replacement word in here" "replacement word in here", "replacement word in here"} --replacement words for filterWords3
	
}

also I want to be able to add more filterWords like filterWords4 and replaceWords4. Let me know if you need some more information

local wordTable = {
 ["ExampleWord"] = {
    "Word1",
   "Word2"
 }
}

function replaceWord(word)
 local newWord = wordTable[word][math.round(math.random(1,#wordTable[word]))]
 return newWord
end)

Yeah but I want 2 tables, and when a word is said in the wordTable1 or wordTable2, it will choose a random word from replacementWords1 or replacementWord2 depending on what word they said, look at the example I gave

You can use Player.Chatted Event.

For example.

game.Players.PlayerAdded:Connect(function(plr)
	local tab = {
		Word1 = {Good = "Alr", Bad = "NO"},
		Word2 = {Good = "Good", Bad = "Bad"}
	}
	
	plr.Chatted:Connect(function(chat)
		for _,v in pairs(tab) do
			if chat == v.Good then
				print(v.Bad)
			elseif chat == v.Bad then
				print(v.Good)
			end
		end
	end)
end)

image

1 Like

Would this still work if I had multiple strings in Words1 and Words2

game.Players.PlayerAdded:Connect(function(plr)
	local tab = {
		Word1 = {Good = "Alr", Bad = "NO"},
		Word2 = {Good = {"Apple","Orange"}, Bad = {"Are bad","NO ew..", "Nope."}}
	}

	plr.Chatted:Connect(function(chat)
		for _,v in pairs(tab) do
			for i = 1,#v.Good do
				if chat == v.Good[i] then
					print(v.Bad[math.random(1,#v.Bad)])
				end
			end
			for p = 1,#v.Bad do
				if chat == v.Bad[p] then
					print(v.Good[math.random(1,#v.Good)])
				end
			end
		end
	end)
end)

image

Also good luck for Toxic chat game.

One thing, why not just use

table.find()

To see if a word is listed instead of looping so much?
I feel like it’d make the script look cleaner:

game.Players.PlayerAdded:Connect(function(plr)
	local tab = {
		Word1 = {Good = "Alr", Bad = "NO"},
		Word2 = {Good = {"Apple","Orange"}, Bad = {"Are bad","NO ew..", "Nope."}}
	}

	plr.Chatted:Connect(function(chat)
		for _,v in pairs(tab) do
			if table.find(v.Good, chat) then
				print(v.Bad[math.random(1,#v.Bad)])
			elseif table.find(v.Bad, chat) then
				print(v.Good[math.random(1,#v.Good)])
			end
		end
	end)
end)

Just a small detail, but personally it makes the code seem cleaner.
Other than that, yeah, it works.

2 Likes

Alright, seems legit my bad sorry.

I rushed and forgot about the table.find

game.Players.PlayerAdded:Connect(function(plr)
	local tab = {
		Word1 = {Good = "Alr", Bad = "NO"},
		Word2 = {Good = {"Apple","Orange"}, Bad = {"Are bad","NO ew..", "Nope.","Veg","Dope"}}
	}

	plr.Chatted:Connect(function(chat)
		for _,v in pairs(tab) do
			if table.find(v.Good, chat) then
				print(v.Bad[math.random(1,#v.Bad)])
			elseif table.find(v.Bad, chat) then
				print(v.Good[math.random(1,#v.Good)])
			end
		end
	end)
end)

It’s fine! Just thought i’d bring that up since it (at least to me) make the code feel far cleaner.

I get an error: Attempt to index a nil value

heres my table

local filterWords = {
	filterWords1 = { 
		word = {"words", "words", "words", "words",  
		replace = "words", "words", "words", "words", "words", "words", "words", "words"}
	},
	
	filterWords2 = {
		words = {"words",
		replace = "words"},
	},
	
	filterWords3 = {
		words = {"words",
		replace = "words"},
	},
}

for index, catergory in pairs(filterWords) do
		print(catergory)
		for i = 1, #catergory.word do -- this is the line with the error
			print(i)
			if messageObject.Message == catergory.word[i] then
				
			end
		end
	end
local filterWords = {
	filterWords1 = { 
		words = {"words", "words", "words", "words"},  
		replace = {"words", "words", "words", "words", "words", "words", "words", "words"}
	},

	filterWords2 = {
		words = {"words"},
		replace = {"words"}
	},

	filterWords3 = {
		words = {"words"},
		replace = {"words"}
	},
}

for index, catergory in pairs(filterWords) do
	print(catergory)
	for i = 1, #catergory.word do -- this is the line with the error
		print(i)
		if table.find(catergory.words,messageObject.Message) then
			print(catergory.replace)
		end
	end
end
1 Like

invalid argument #1 to find (table expected, got nil) line 32

well for me its only 30 lines…

1 Like

this line if table.find(v.Good, chat) then

nvm! i got it working!!! thanks guys :smiley:

1 Like

last thing, i tried adding in string.find() but It didnt work, how do i add this in?? @imAlex_30 @HatchVerseRBLX :slight_smile:

if table.find(catergory.word, messageObject.Message) or table.find(catergory.word, messageObject.Message:find(catergory.word)) then

Well…

table.find(catergory.word, messageObject.Message)

is fine.

But if you wanna use :find then

for i = 1,#category.word do
  table.find(catergory.word, messageObject.Message:find(catergory.word[i]))
end

but if they say something like APPLE, it wont pick it up so Im using string.upper, how do i implement string.upper()

This should be able to help you with finding just the word!

Also to most recent reply: it’s variable:upper() and variable:lower() like:

('HI'):lower() -- hi
('hi'):upper() -- HI

alright, this is what I’ve tried but it outputs attempt to call a nil value @HatchVerseRBLX @imAlex_30

if table.find(catergory.word, messageObject.Message) or table.find(catergory.word, messageObject.Message:upper():find(catergory.word:upper())) then