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

How to make chat door answer to multiple answers?

Asked by 2 years ago
Edited by chess123mate 2 years ago

I don't know much about scripting but I'm trying to make it where if a person says for example "cat" and or "dog" the door will open. I'm having trouble getting it to take more than one correct answer and this is the code that I am using. I don't know if I should be using stringvalue for this or what I should change in order for it to work.

door = script.Parent
function onChatted(msg, recipient, speaker) 
    local source = string.lower(speaker.Name) 
    msg = string.lower(msg) 
    local thecharacter = script.Parent.TheCharacter
    local decal = script.Parent.Decal
    if (msg == string.lower(thecharacter.Value)) then 
        door.CanCollide = false 
        door.Transparency = 0.7 
        decal.Transparency = 0.7 (IMPORTANT) 
        wait(2)
        decal.Transparency = 0(IMPORTANT)
        door.CanCollide = true 
        door.Transparency = 0 
    end 
end 
game.Players.ChildAdded:connect(function(plr)
    plr.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, plr) end) 
end)
0
please use the correct lua formating! NarwhalAndMe 141 — 2y
0
I've edited the question to use the code block (you can add it yourself in the future with the little "Lua" icon to the right of Bold/Italics/etc). chess123mate 5873 — 2y
0
Sorry I wasn't quite sure how to format it correctly hyuckssunn 2 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
  • Declaring variables local is best practice (due to how Lua works it is slightly faster). This applies to functions, too. Just note that you can't reference a local variable until after the statement is complete, so if you have two local functions that need to reference each other, you need to "forward declare" at least one of them. Example:
-- Wrong way
local function first(callSecond)
    print("In first")
    if callSecond then
        second() -- "second" is declared below and so isn't available here
    end
end
local function second(callFirst)
    print("In second")
    if callFirst then
        first()
    end
end

-- Right way: forward declare 'second'
local second
local function first(callSecond)
    print("In first")
    if callSecond then
        second() -- "second" is declared below and so isn't available here
    end
end
function second(callFirst)
    print("In second")
    if callFirst then
        first()
    end
end
  • PlayerAdded is recommended over ChildAdded
  • connect is deprecated; Connect is recommended instead
  • You can use a StringValue to support multiple answers but a table is probably easier to edit in this case:
local answers = {
    ["cat"] = true,
    ["dog"] = true,
}
local door = script.Parent
local decal = door.Decal
function onChatted(msg, recipient, speaker) 
    msg = string.lower(msg) 
    if answers[msg] then
        door.CanCollide = false 
        door.Transparency = 0.7 
        decal.Transparency = 0.7
        wait(2)
        decal.Transparency = 0
        door.CanCollide = true 
        door.Transparency = 0 
    end 
end 
game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, plr) end) 
end)
Ad

Answer this question