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

Why is this script detecting words out of context?

Asked by
chrono2 189
8 years ago

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

Log in to vote
1
Answered by 8 years ago

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) 

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. chrono2 189 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Please provide code with your answers. Simply posting an explanation does not help someone new to programming understand how to implement a concept programatically.
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.

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. chrono2 189 — 8y

Answer this question