I’m making a puzzle game where there’s a room that has a riddle on a wall. When a player says the answer to the riddle in chat, the door opens. However, when I test it out, I get the error "Attempt to index nil with “Chatted.”
riddle = math.random(1, 10)
answer = "e"
if riddle == 1 then
script.Parent.SurfaceGui.TextLabel.Text = "I have 104 keys, but only 3 locks, only one enter and only one escape, and I have space but no room. What am I?"
answer = "keyboard"
elseif riddle == 2 then
script.Parent.SurfaceGui.TextLabel.Text = "I have a neck, a lip, a cap, but no head. What am I?"
answer = "bottle"
elseif riddle == 3 then
script.Parent.SurfaceGui.TextLabel.Text = "I belong to you, but other people use it more than you do. What am I?"
answer = "name"
elseif riddle == 4 then
script.Parent.SurfaceGui.TextLabel.Text = "I come in a minute, twice in a moment, but never in a thousand years. What am I?"
answer = "m"
elseif riddle == 5 then
script.Parent.SurfaceGui.TextLabel.Text = "I can be the size of an elephant and not weigh an ounce. What am I?"
answer = "shadow"
elseif riddle == 6 then
script.Parent.SurfaceGui.TextLabel.Text = "I can be broken just by saying my name. What am I?"
answer = "silence"
elseif riddle == 7 then
script.Parent.SurfaceGui.TextLabel.Text = "I am an invention that lets you see through walls. What am I?"
answer = "window"
elseif riddle == 8 then
script.Parent.SurfaceGui.TextLabel.Text = "I can be cracked, made, told and played. What am I?"
answer = "joke"
elseif riddle == 9 then
script.Parent.SurfaceGui.TextLabel.Text = "I have cities, but no houses. I have mountains but no trees. I have water but no fish. What am I?"
answer = "map"
else
script.Parent.SurfaceGui.TextLabel.Text = "I can run, but I have no legs. I have a bank, but no money. I have a mouth, but no tongue. What am I?"
answer = "river"
end
local player = game.Players.LocalPlayer
local door = script.Parent.Parent.Door
player.Chatted:connect(function(msg)
if string.sub(string.lower(msg), 1,8)==string.lower(answer) then
door.Transparency = 0.8
door.CanCollide = false
wait(2)
door.Transparency = 0
door.CanCollide = true
end
end)