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

How to make chat door answer to multiple answers?

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)

3 comments

0

please use the correct lua formating!

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).

0

Sorry I wasn't quite sure how to format it correctly

1 answer

✓ Accepted answer
  • 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)

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