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

Why is this script detecting words out of context?

This script is supposed to detect when a player says anything with the words "What", "He", and "Do", or "What", "It", and "Do", or "What", "You", and "Do".

However, while it does so just fine, it also detects "He", "It", and "You" in any context with or without the other words.

		words = {
--["Explain Function"]
		words = {
			{"What","It","Do"},
			{"What","He","Do"},
			{"What","You","Do"},
		},
		        func = function( p ) -- functions go HERE
            print(p.Name .. " asked Faraday to explain his function")
			game.ServerScriptService.FaraVoice.Value = "Currently I am programmed to respond to certain phrases, however soon I will run this house."
			wait(0.3)
			local text = game.ServerScriptService.FaraVoice.Value
			local y = game:GetService("Chat"):Chat(Interfaces, ""..text, Enum.ChatColor.Green)  
        end
}
}
function onChatted(msg, recipient, speaker)
	print("chatted")
	local source = speaker
	msg = string.lower(msg)
	print(#phrases,type(phrases))
	for a=1,#phrases do--search command types
		local _a = phrases[a].words
		for b=1,#_a do -- search words
			local _b = _a[b]
			if(msg:match(string.lower(_b[2])))then
				print(msg,_a[2])
				phrases[a].func(source)
				return
			end
		end
	end
end
function onPlayerEntered(newPlayer)
	newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end)
end
game.Players.ChildAdded:connect(onPlayerEntered)

2 answers

✓ Accepted answer

You altered the code I gave you in a way that makes it work really differently. I don't really even know what your word sort algorithm results in. Anyways, I updated the same one I made you yesterday to do what you want.

Should be straightforward

match = nil

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

		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")
		
		
		for b,wT in pairs(v.words) do
			local allFound = true
			if match ~= nil then break end
			print("# words = " .. #wT)
			for u,c in pairs(wT) do
				if not msg:match(string.lower(c)) then
					allFound = false
				else
					--print("match = " .. string.lower(c))
				end
			end
			if allFound and match == nil then
				match = v
				--print("match = " .. tostring(v))
			end
		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

Thanks for that. I basically just used your code as a template to make my own with some help from a friend, because I couldn't understand yours well enough to make it do what I wanted it to. I thought I PMed you about it, but I guess I was wrong. Anyway, thanks again! You're the reason this project is even possible.

function onChatted(message, recipient, speaker)
	local actualRecipient = "Admin"
	if recipient == actualRecipient then
		--The rest of your code there.
	end
end

That's because it happens anytime when the player chats.

I believe the solution is for you to specify the recipient that should respond and if the recipient argument matches the variable, then the function executes.

The above code does that for you. You can paste your code within the if/then. If the recipient is the wanted recipient, then your function should check the words.

1 comment

0

How would that change anything though? It works fine if I'm using only 2 word phrases, but when I put in a third it detects it without the other two.

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