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)
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
connect
is deprecated; Connect
is recommended insteadlocal 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)