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

How do I use onChatted to check for multiple messages for the same function?

I'm writing a very rudimentary pseudo-AI script which in essence is used for roleplay purposes only. It's designed to detect a series of sentences and respond accordingly.

Let's say that I want to turn it into Jarvis from Iron Man, and I want it to activate a suit morph or something whenever it detects that I say the words: "Jarvis" and "Suit" in any order or context, or "Jarvis" and "Armour" in any order or context.

How would I code that event?

1 answer

✓ Accepted answer

This is a script that looks for keywords in phrases. If your phrase has ALL of the keywords in it,

for exampleJarvis + Armour or even Jarvis + heal,

it will run the function for the corresponding phrases. The functions for each phrase are inside the table phrases

This should be pretty straightforward but pls let me know if you have any questions - my code can be confusing

match = nil

phrases = {
	["Amour"] = { 
		words = {"Jarvis" , "Armour"},
		func = function( p ) -- functions go HERE
			print(p.Name .. " activated armor function")	
		end
	},
	["Health"] = {
		words = {"Jarvis","Heal"},

		func = function( p ) -- functions go HERE
			print(p.Name .. " activated health function")	
		end

	}
}


function onChatted(msg, recipient, speaker) 

	print("chatted")
	local source = speaker
	msg = string.lower(msg) 
	
	match = nil
	
	for i,v in pairs(phrases) do
		if match~=nil then break end
		print("no match, scanning")
		
		local allFound = true
		for u,c in pairs(v.words) do
			if not msg:match(string.lower(c)) then
				allFound = false
			end
		end
		if allFound and match == nil then
			match = v
			print("match = " .. tostring(v))
		end
	end
	
	if match then
		print("match found, running function")
		match.func(source)
		match = nil
	end

end 

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered) 

1 comment

0

That works perfectly! Exactly what I needed. I can learn a lot from this.

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