It's not the lack of comments that bugs me so much as the lack of formatting - books are written in paragraphs for good reason, and unspaced code likewise reads as an amorphous blob of text. Stick an extra line break between your blocks.
If I were you, before attempting to troubleshoot the problem I'd be refactoring the whole script into something shorter. You'll find it's a lot easier to track where things are going wrong.
Don't define functions inside your loops. If you tell Lua to rebuild a function over and over again, it'll do exactly that. If a declaration only needs to be peformed once, then move it near the top of your code.
A dedicated writing function would be advantageous. Here's one I've often used:
local function writeAt(text, x , y, textCol, bgCol)
if textCol then term.setTextColour(textCol) end
if bgCol then term.setBackgroundColour(bgCol) end
term.setCursorPos(x, y)
term.write(text)
end
This allows you to turn this sort of thing:
term.setCursorPos(21,8)
print("| |")
term.setCursorPos(21,9)
print("| |")
term.setCursorPos(21,10)
print("| |")
term.setCursorPos(21,11)
print("+--------+")
term.setCursorPos(33,11)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print("Arrows: Select Card")
… into this sort of thing:
writeAt("| |", 21, 8)
writeAt("| |", 21, 9)
writeAt("| |", 21, 10)
writeAt("+--------+", 21, 11)
writeAt("Arrows: Select Card", 33, 11, colours.white, colours.black)
Further use of tables is nigh-on essential for keeping these sort of scripts to a manageable size. For example, say we built a table out of this lot:
--Dealing!
play1 = {""}
play2 = {""}
play3 = {""}
play4 = {""}
robot1 = {""}
robot2 = {""}
robot3 = {""}
… like so:
plays = {{""}, {""}, {""}, {""}}
robs = {{""}, {""}, {""}}
… then we could shrink this sort of thing:
deal(play1)
if players >= 2 then
deal(play2)
end
if players >= 3 then
deal(play3)
end
if players == 4 then
deal(play4)
end
if robots >= 1 then
deal(robot1)
end
if robots >= 2 then
deal(robot2)
end
if robots == 3 then
deal(robot3)
end
… down like so:
for i = 1, players do deal(plays[i]) end
for i = 1, robots do deal(robs[i]) end
Likewise, a table like this:
local shorts = {["red"] = "r", ["blue"] = "b", ["green"] = "g", ["yellow"] = "y"}
… lets you turn the likes of this:
repeat
inputcolor = string.lower(read())
until inputcolor == "red" or inputcolor == "blue" or inputcolor == "green" or inputcolor == "yellow"
if inputcolor == "red" then
table.insert(discard, "r"..playcard[2])
elseif inputcolor == "blue" then
table.insert(discard, "b"..playcard[2])
elseif inputcolor == "green" then
table.insert(discard, "g"..playcard[2])
elseif inputcolor == "yellow" then
table.insert(discard, "y"..playcard[2])
end
… into something much simpler:
repeat
inputcolor = string.lower(read())
until shorts[inputcolor]
table.insert(discard, shorts[inputcolor]..playcard[2])
In terms of the original problem, you'll want to be a bit more
specific. Where, to the line, do the values change in an unexpected manner? What sort of values are expected, and what exactly do you get instead? If you suspect you're getting a nil value where there shouldn't be one, use the type() function to
check that.