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

Help With If Statements

Started by lastai, 30 July 2013 - 04:59 PM
lastai #1
Posted 30 July 2013 - 06:59 PM
I'm currently working on a dj script
I'm a DJ at an internet radio station and we just got a MC sever
we add costum music discs and they run in CC
now we are trying to set up a looping system that can loop at corret time for each disc as well as shuffle


while(true)do
  a = math.random(0,5)
  if a = 0 then b = 100 end
  if a = 1 then b = 255 end
  if a = 2 then b = 175 end
  if a = 3 then b = 126 end
  if a = 4 then b = 78 end
  if a = 5 then b = 160 end
  shell.run("dj", "play drive_"a)
  sleep(B)/>/>
end
someith is wrong with my if statments but i can figure out what
ive look through several tutorials but they should be right. im missing something
so i was hoping some might see what i dont.
Lyqyd #2
Posted 30 July 2013 - 11:53 PM
Split into new topic.

Use the comparison operator (==) for your if conditions instead of the assignment operator (=).
campicus #3
Posted 31 July 2013 - 01:03 AM
Therefore:


if a == 0 then
  b = 100
end

etc
Smiley43210 #4
Posted 31 July 2013 - 02:55 AM
What campicus said, but keep in mind that if statements can still be one line statements.
And if you missed what campicus said, let me point it out for you:
He wrote   if a == 0 then b = 100 end.
You wrote if a = 0 then b = 100 end.
To say if something equals something else, you must use two equals signs, and not just one.

-snip-
Something is wrong with my if statements but i can figure out what.
And, something is wrong with your string concatenation. You have
shell.run("dj", "play drive_"a)
Which should be changed to
shell.run("dj", "play drive_"..a)
TheOddByte #5
Posted 04 August 2013 - 03:04 PM
Even though your if statements aren't so wrong except that it should be '==' instead of '=' when checking if it's equal, I'm wondering.. Why don't you use 'elseif' statements?
Example:

local input = read() --I think you pretty much know what this does

if input == "Hello" then -- If the input is equal to 'Hello'
 print("Hello to you too!")

elseif input == "Goodbye" then -- If the input is equal to 'Goodbye' instead
 print("See ya!")

else -- If it's not equal to 'Hello' or 'Goodbye'
 print("What?")
end

And when you are doing this you only need 1 end since it only counts as one 'if' statement ;)/>