Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
chrono2 189
7 years ago

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

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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) 

0
That works perfectly! Exactly what I needed. I can learn a lot from this. chrono2 189 — 7y
Ad

Answer this question