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

"invalid argument #2 to 'find' (string expected, got table)". how do i check if its in the table?

My script is

local ScareEnablers = {
	";enable scary mode";
	";enable scary";
	";enable scares";
	";scares enabled";
	";scares on";
}

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg:lower():find(ScareEnablers) then
			local char = plr.Character or plr.CharacterAdded:Connect()
			local hum = char:FindFirstChild("Humanoid")
			hum.WalkSpeed = 9
			print("Working")
		end
	end)
end)

And it fires always. working well. But whenever I try making it a table to get a word from, For example. If a player says something on the table. I get the error "invalid argument #2 to 'find' (string expected, got table)". And i dont know how to fix it. Is there something special I have to do?

1 answer

✓ Accepted answer

The error you're getting is because you're providing the table itself instead of any of the contents inside. You'd need to iterate through the table using a for loop and check if the player's message contains any of those values.

local ScareEnablers = {
	";enable scary mode";
	";enable scary";
	";enable scares";
	";scares enabled";
	";scares on";
}

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		for index, value in pairs(ScareEnablers) do
			if msg:lower():find(value) then
				local char = plr.Character or plr.CharacterAdded:Connect()
				local hum = char:FindFirstChild("Humanoid")
				hum.WalkSpeed = 9
				print("Working")
				break
			end
		end
	end)
end)

Another way of doing this is by using table.find which will check if the given element is inside the specified table. This will return the index position of the element that is inside the table or, it will return nil if it cannot find the element inside the table. You can see more about table.find here (scroll down to the bottom)

local ScareEnablers = {
    ";enable scary mode";
    ";enable scary";
    ";enable scares";
    ";scares enabled";
    ";scares on";
}

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if table.find(ScareEnablers, msg:lower()) then
            local char = plr.Character or plr.CharacterAdded:Connect()
            local hum = char:FindFirstChild("Humanoid")
            hum.WalkSpeed = 9
            print("Working.\nIndex Position:",table.find(ScareEnablers, msg:lower()))
        end
    end)
end)

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