This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ztc0611's profile picture

Program Ignoring if Statements

Started by ztc0611, 30 May 2016 - 03:37 AM
ztc0611 #1
Posted 30 May 2016 - 05:37 AM
Spoiler

local joke = 1
local start = 0
term.setBackgroundColor(colors.white)
term.clear()
term.setCursorPos(19,7)
term.setTextColor(colors.blue)
print("Sara v0.2.5")
term.setCursorPos(18,9)
term.setTextColor(colors.red)
print("SYSTEMS OFLINE")
term.setCursorPos(9,13)
term.setTextColor(colors.black)
print("type 'sara on' in chat to power on")
if start == 0 then
local event, right, player, msg = os.pullEvent("chat_message")
  if msg == "sara start up" or "sara power on" or "sara turn on" or "sara on" then
  start = 1
  end
end

c = peripheral.wrap("right")

term.setCursorPos(18,9)
term.setTextColor(colors.purple)
print("SYSTEMS STARTING")

c.setLabel("Sara")
c.say("Powering Up...")
sleep(math.random(2.,5))
c.say("Powerup Complete!")

term.setCursorPos(18,9)
term.setTextColor(colors.green)
print(" SYSTEMS ONLINE ")

while true do

  local event, right, player, msg = os.pullEvent("chat_message")

if msg == "sara hello" or "sara hi" then
  local res1 = math.random(1,2)
  sleep(math.random(0.5,1))
   if res1 == 1 then
   c.say("Hello Sir")
   end
   if res1 == 2 then
   c.say("Hello There")
   end
end

if msg == "sara joke" or "sara tell me a joke" then
  sleep(math.random(1.5,3))

   if joke == 6 then
	c.say("No more Jokes, Restarting from beginning")
	   joke = 0
   end
  
   if joke == 5 then

	   joke = joke+1
   end
  
   if joke == 4 then

	joke = joke+1
   end
  
   if joke == 3 then

	joke = joke+1
   end
  
   if joke == 2 then

	joke = joke+1
   end
  
   if joke == 1 then
	c.say("Can a kangaroo jump higher than a house?")
	sleep(3)
	c.say("Of course, a house doesn't jump at all.")
	joke = joke + 1
   end
  
if msg == "sara enable lamp" then
  c.say("Enabling Redstone on Top Side")
  redstone.setOutput("top", true)
end

if msg == "sara open doors" then
  c.say("Opening Doors")
  redstone.setOutput("left", true)
  sleep(6)
  c.say("Closing Doors")
  redstone.setOutput("left", false)
end

else
  c.say("error")
end

end


Doing typing anything just boots it up, and typing anything after that will always tell you a joke.
Edited on 30 May 2016 - 04:43 PM
Bomb Bloke #2
Posted 30 May 2016 - 10:02 AM
This doesn't do what you think it does:

if msg == "sara start up" or "sara power on" or "sara turn on" or "sara on" then

It acts like this:

if (msg is "sara start up") or ("sara power on" is non-nil) or ("sara turn on" is non-nil) or ("sara on" is non-nil) then

Those strings are always non-nil, so the line always resolves as true.

You meant:

if msg == "sara start up" or msg == "sara power on" or msg == "sara turn on" or msg == "sara on" then
ztc0611 #3
Posted 30 May 2016 - 04:07 PM
Thank you so much! :D/>