Native Roblox Lua AI Algorithm

I have developed a VERY simple, lightweight program to take inputs, match them with patterns in a pre-defined network, and then predict which category the input may belong to. The net is not trainable, nor is it the most reliable. However, I believe that this may be the first step to creating a true system that would have realistic human-like predictions based on text inputs.

The whole system is based on a Python project found on YouTube here.

The algorithm script, placed in ServerScriptService:

-- Script
local Net = require(game.ServerScriptService.WordNet)

local IGNORE_LETTERS = {"!", "?", ".", "'"}

local function ProcessInput(Input: string): string
	-- Manage the formatting of the input string
	Input = string.lower(Input)
		
	-- Set a undefined, accessable variable
	local Matches = {}
	
	-- Find matches for different patterns
	for i, Category in pairs(Net) do
		for _, Pattern in pairs(Category["Patterns"]) do
			if string.match(Input, Pattern) ~= nil then
				table.insert(Matches, i)
			end
		end
	end
	
	table.sort(Matches)
	
	local ChosenCategory = nil
	local HighestNumber = 0
	local CurrentChosen = nil
	local CurrentNumber = 0
	
	-- Find the percentage match based on the results of the match finding
	for i, Category in pairs(Matches) do
		if CurrentChosen == Net[Category] and CurrentNumber > 0 then
			CurrentNumber += 1
			if HighestNumber < CurrentNumber then
				HighestNumber = CurrentNumber
				ChosenCategory = Net[Category]
			end
		elseif CurrentChosen == Net[Category] and CurrentNumber < 1 then
			CurrentNumber = 1
			if HighestNumber < CurrentNumber then
				HighestNumber = CurrentNumber
				ChosenCategory = Net[Category]
			end
		elseif CurrentChosen ~= Net[Category] and ChosenCategory == nil and HighestNumber < 1 then
			CurrentNumber = 1
			CurrentChosen = Net[Category]
			ChosenCategory = Net[Category]
			HighestNumber = 1
		elseif CurrentChosen ~= Net[Category] then
			CurrentNumber = 1
			CurrentChosen = Net[Category]
		end
	end
			
	if ChosenCategory ~= nil then
		local OutcomeSelection = math.random(1, #ChosenCategory["Outcomes"])
		return ChosenCategory["Outcomes"][OutcomeSelection]
	else
		return "Hmm... I don't think I know that one yet. I am still being trained and can't understand everything quite yet."
	end
end

local ChatEvent = game.ReplicatedStorage.Chat

ChatEvent.OnServerEvent:Connect(function(Player: Player, Message: string)
	task.spawn(function()
		print(Player, Message)
		ChatEvent:FireClient(Player, ProcessInput(Message))
	end)
end)

The Network itself, currently only containing 3 categories:

-- ModuleScript
local Net = {
	["Greetings"] = {
		["Patterns"] = {"hello", "hi", "hey", "im back"},
		["Outcomes"] = {"Hello!", "Hi! How are you?", "Hey!", "Hey! Nice to see you!", "Good to see you!", "Hey! How have you been?", "What's up!"}
	},
	["Goodbyes"] = {
		["Patterns"] = {"bye", "cya", "see ya", "goodbye", "im leaving"},
		["Outcomes"] = {"Goodbye!", "Have a good one!", "Sorry to see you leave me...", "Oh, that's okay I guess :("}
	},
	["Feel Good"] = {
		["Patterns"] = {"feel great", "feel good", "feel happy", "feel delighted"},
		["Outcomes"] = {"That's great to hear!", "Wonderful!", "Great!", "Sweet!", "Happy day!"}
	},
	
}

return Net

Here is the test place if you would like to try out the current system.

This system is incredibly basic and is not in any way a finished product…

5 Likes

This is not an AI, though. It is a normal code.

8 Likes

It might seem like that, but this is in fact a very simple AI. You can provide input which are not in the patterns table, and it will still work.

The responses are not generated, they’re premade. The inputs are what the ai is interpreting. You can always add more categories to make it seem more advanced… i.e a “Weather question” category might look like this:

["Weather Question"] = {
		["Patterns"] = {"hows the weather", "whats the weather", "how weather", "is it hot"},
		["Outcomes"] = {"Its always sunny in Roblox!", "The weather is just like yesterday!", "Same as tomorrow!"}
	},
3 Likes

It’s really not an AI, not even simple, it is just normal code. Fuzzy search.
It is not an AI because it does not learn about the phrases or patterns in them, but instead does a small fuzzy search.

4 Likes

By definition, it is an ai. It is performing actions based on inputs which it has not been explicitly given directions for.

3 Likes

That is essentially what AI is… At this point in time, it is just standard code that predicts the most accurate outcome of an input. True AI would be able to think, process, and understand the meanings and intentions of each word. Thank you for the reply though!

2 Likes

Actually, the code is even worse than I thought. I hadn’t read it before posting that comment, but now that I’ve read it, I see that it’s even worse.

This code essentially boils down to a table of input sentences with predefined responses. It doesn’t even perform fuzzy searching through words to check compatibilities, which means that if you make even a single typo in a word, it will claim not to know that sentence.

This code doesn’t come close to the complexity of an AI, be it simple or, let alone, complex. Besides that, it is a fact that the code is heavily unoptimized, performing sentence sorting at runtime to check for exact word matches.

In essence, it is:

local Inputs = {"hi", "hello"}
local Outputs = {"Hello, how are you?", "Hi, how are you doing?"}

function Process(text: string): string
    if Inputs[text] then
       return Outputs[math.random(1, #Outputs)] -- Random output?
    end
   
   return "I don't know this one yet."
end

It’s evident that the code does more than just this, although most of what it does is unnecessary. For instance, classifying the sentence by ‘category’ doesn’t add any utility to the code, as it only returns a string without its category. This could be optimized by sorting the sentences when the script begins running, rather than performing it in every function call.

If you’re interested, I’d like to engage in a small ‘code 1v1’ focused on creating the same ‘input’ handler but with performance and efficiency tests to determine correct responses and select the winner.

2 Likes

Blud turned into a competitive egoist :skull:
The poster made it clear that it isn’t a finished product, and just a prototype example for stuff that can be made, no need to get so heated just because the code doesn’t fit your standards.

3 Likes

Well so far it isn’t an AI, it’s just an algorithm to choose responses.

3 Likes

a neural network is basically just an algorithm with learned weights and biases
i guess this barely counts

1 Like

Yep. All the guys above don’t know anything about AI’s. Heck, AI isnt even a real term. You can define it as you want. But 99% of the time people refer AI as Machine Learning, Q learning or Deep Neural Networks.

In this case, the script OP provided is a Q-table. Q-tables are used for Q learning, but OP doesn’t provide any method for the “AI” to learn in his script.

3 Likes

And for OP, please take this as constructive criticism.

Q-tables are the most important part of Q-learning. Q learning basically means you give it a input. If it exists in the Q-table, then return it. If not, then try to randomise the correct response. If it did right, save it into the Q-table. If it did wrong, add it to a list of wrong answers to that question and when the question appears again try to get a good response, based on the list of wrong answers.

However, Q-tables take a lot of memory and will become useless, for example, with 5 inputs each that take a number from 0 to 5, it would take 3125 guesses till it gets the correct response. And when any input is changed even by 1, you would have to redo it all again.

Deep Q learning (My favorite Machine Learning method), is a little bit more complicated.

Instead of Q-tables, you have a deep neural network.

The input layer of the neural network receives the input, the hidden layers do their magic, and the response layer gives you the correct response.

And you do lets say 10 questions, and then reward or punish the bot correctly, after the training session you change the biases and weights of each neuron based on previous training and most importantly this training session.

You can see this video to learn how neural networks work. Its not really specific about Deep Q Learning, but the idea is the same. https://www.youtube.com/watch?v=IHZwWFHWa-w&t=1033s&ab_channel=3Blue1Brown

1 Like

Generally, most programmers define state machines and algorithms like these as AI. You’re probably referring to the shiny marketing definition of AI where a complex mathematical model can make your life easier or do something really cool.

I don’t know why Community Resources who refer to AI always end up in a discussion about how we define AI.

2 Likes

heated?

Okay. But im not an egoist, i like competitions and have never done one about coding.

2 Likes

First of all, wow. I didn’t think something like this would be argued this much…

I am aware that this is not truly AI (Artificial Intelligence) and that was never its true intent. The keyword there is “intelligence”. The program is not intelligent at all. It does what it’s told to do. It performs the very basic function of an input-output program. I just wanted to make something like what I have seen and share it. Obviously, the Python project uses tokenizing and matching, but right now (for the 30 minutes I spent making it in Studio) this is not bad.

I would like to point out that this is not something that I want to compete for (although I would love to see what you can create that is truly “AI”) this is simply a chatbot using a “Q-table” (I think I am using that correctly? @Bloxxy213_DVL) and checking to see if it exists in the table. (It’s not really “AI” but it acts similarly)

Thanks for your time, and I would love to see what you’re capable of! (Make me impressed with a true “AI” built with Roblox Lua. - I bet you can’t…)

1 Like

Yep, this is correct usage.

Not exactly. Thats deep q learning when referring to machine learning. You provide rewards or punishments for actions and their states, and it learns what to do or what not to do in certain situations, following the gradient descent, and changing its weights and biases accordingly.

Its not that complicated to build a neural network. Its been done again and again on Roblox. The complicated part is having enough memory and storage for keeping a giant model, which roblox doesnt have.

2 Likes