-1

# Door that opens when you say something help?

My game is a Dragon Ball Quiz and the first door is supposed to open when you say "dragon ball" but I also want it to open if you say "dragonball" and when I tried to edit it for more than one phrase, the line for it went red. Here's the entire script: door = script.Parent --this is the door

function onChatted(msg, recipient, speaker)

-- convert to all lower case

local source = string.lower(speaker.Name)
msg = string.lower(msg)

if (msg == "dragon ball", "dragonball") then --This line turned red when I added , "dragonball"
door.CanCollide = false
door.Transparency = 0.7
wait(5)
door.CanCollide = true
door.Transparency = 0
end

end

function onPlayerEntered(newPlayer)
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end)
end



Any help would be appreciated, if it can't be fixed, it's ok I can just have it say one thing. :)

-1

I think you would do

function onChatted(msg, recipient, speaker)

-- convert to all lower case

local source = string.lower(speaker.Name)
msg = string.lower(msg)

if (msg == "dragon ball" or msg == "dragonball") then -- I think you meant to add or not a comma.
door.CanCollide = false
door.Transparency = 0.7
wait(5)
door.CanCollide = true
door.Transparency = 0
end

end

function onPlayerEntered(newPlayer)
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end)
end



Let me know if this fixes it.

0
Thank you greatly Star! :D Xeron750 0 — 7y
0
BlueTaslem 18044
7 years ago

In English, we say

-- THIS IS WRONG FYI
if A is 1 or 2 then


However, in Lua, the order of operations (for good reason) makes that look like this:

if (A is 1) or (2) then


This is definitely NOT what you intended when you wrote like that:

We see that A is actually completely unrelated to the value 2 in this condition (and because of ho or works in Lua, this statement will actually always evaluate to something true-y, so this mistake actually makes the condition completely useless).

We instead, unfortunately, have to repeat the whole statement:

if (A is 1) or (A is 2) then


Which, in your specific case, would be written like this:

if msg == "dragon ball" or msg == "dragonball" then


There are slightly more powerful ways to do this which might prove useful, however. For instance, if you started by removing all spaces from msg in the first place, the check for the space/unspaced versions would be unnecessary, since all spaces were removed. We would do this in the line that makes it lowercase:

msg = string.lower(msg):gsub("%s","");
-- %s is any space, we replace it with "" (nothing)
-- thus removing any spaces.

0
Dont downvote a perfect reply. Nickoakz 231 — 7y
1
I'm not going to downvote it, I have no reason to, but I just see the other answer as a bit more simple, I'm not too good a scripting.. Xeron750 0 — 7y
0
The other answer is wrong, this answer is correct, provides explanation, and alternatives. BlueTaslem 18044 — 7y