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

Lua Scripting Problems?

Started by Silent_Potato, 09 August 2012 - 10:18 AM
Silent_Potato #1
Posted 09 August 2012 - 12:18 PM
So recently, i started playing around with ComputerCraft quite a bit and i kinda get the ropes of it. Im the owner of a tekkit server and am trying to make a program that will allow me to "execute" things to work my mob arena; For starters, your suppost to type in howmany players are going to play. After that, then it's suppost to "Pulse" through the back to redstone to activate something that will drop items in a certain place, in which it will drop more if you put in a different number (AKA, 4 sets of items for "4" players.)Afterwards, you type "Start" into it to begin the mob arena (No clue how ima go about this, but i bet its more than possible.) Im also gonna use Eloraams Redpower bundled cables to make a changing environment; such as maybe 10 minutes in it will start making little lava fountains appear from the ceiling; or even maybe water? anyways i started making the code, and i ran into this Error: BIOS:204: [string "startup"] :39: 'THEN' EXPECTED: I know this seems self explanatory; but i don't get how else i would make it perform that certain action without an "If" instead of "then". Can Anyone Help? Here is my script:

while true do
Print ("Welcome to the Mob Arena!")
Print ("Please type howmany players you have! [Up to a maximum of 4]")
term.clear()
term.setCursorPos(1, 1)
input = io.read()
if input == 1 then
redstone.setOutput("back", true)
sleep(1)
redstone.setOutput("back", false)
elseif input == 2 then
local x = 1
repeat
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
x = x + 1
until x == 2
elseif input == 3 then
local z = 1
repeat
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
z = z + 1
until z == 3
elseif input == 4 then
local f = 1
repeat
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
f = f + 1
until f == 4
elseif print ("Invalid Number")

if local x = 2
print ("Type "Start" to begin the mob arena")
elseif local z = 3 then
print ("Type "Start" to begin the mob arena")
elseif local f = 4 then
print ("Type "Start" to begin the mob arena")
elseif local k = 1 then
print ("Type "Start" to begin the mob arena")


The ERROR starts after that gap between the two scripts. thanks for any help you can give!
Cloudy #2
Posted 09 August 2012 - 12:25 PM
Use "else" instead, you don't need elseif. Make sure you end the statement after that.

Also, I note you use repeat statements and increment a variable inside. Instead, use a for statement:


for i = 1, 4 do
--repeated stuff here
end
Silent_Potato #3
Posted 09 August 2012 - 02:34 PM
while true do
Print ("Welcome to the Mob Arena!")
Print ("Please type howmany players you have! [Up to a maximum of 4]")
term.clear()
term.setCursorPos(1, 1)
input = io.read()
end
if input == 1 then
for k = 1, 1 do
redstone.setOutput("back", true)
sleep(1)
redstone.setOutput("back", false)
end
if input == 2 then
for x = 1, 2 do
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
end
if input == 3 then
for z = 1, 3 do
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
end
if input == 4 then
for f = 1, 4 do
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
end
else print ("Invalid Number")
end
if local x = 2 then
print ("Type "Start" to begin the mob arena")
end
else local z = 3 then
print ("Type "Start" to begin the mob arena")
end
else local f = 4 then
print ("Type "Start" to begin the mob arena")
end
else local k = 1 then
print ("Type "Start" to begin the mob arena")
end

Now it says "Unexpected symbol near "local" on line 37 D=
so what the hell did i screw up purposely (not literally) this time?
I've tried every version of things on line "37" and nothings working, i used elseif, else, then, and if (though i knew these seemed wrong)
Cranium #4
Posted 09 August 2012 - 03:27 PM
When you are using the read() or io.read() function, it takes whatever you type, and turns it into a string. You need to have the inputs == "number here" within quotations. otherwise, nothing will ever equal that. and at the end, when you say if local x = something, you don't have to put local there, only when you are defining x. such as this:

local x = 0 --this is the only time you need to define as local, when it's declared. not when you call to it.
if x = 0 then
print("this worked")
end
Lyqyd #5
Posted 09 August 2012 - 03:45 PM
Equality comparisons do require '==' instead of '='.
Silent_Potato #6
Posted 09 August 2012 - 09:45 PM
Ok, so since i've began this, i've been using a online lua reader. This time when i ran it through, i had no "Code Errors," but instead a runtime error that says
lua: prog.lua:3: attempt to call global 'Print' (a nil value)
stack traceback:
prog.lua:3: in main chunk

Is it doing this because it's not in minecraft or what other reason? Don't want this to be screwed up for others =P
and i am getting ready to make my next line of code that is gonna use algebraic math to calculate how many "Mobs" to spawn. Is there a way to basically tell the program to do an algorithm that will multiply the amount of players to a set amount of mobs, but without having to repeat the same command with tons of variables? Example:

local h = 1 (Sets "Local" h variable)
then do X(# of players) * 5 = h
for j = 1, h do (Defines a value of j and makes it so it will "repeat a command "H" times)

while true do
Print ("Welcome to the Mob Arena!")
Print ("Please type howmany players you have! [Up to a maximum of 4]")
term.clear()
term.setCursorPos(1, 1)
input = io.read()
end
if input == "1" then
for k = 1, 1 do
redstone.setOutput("back", true)
sleep(1)
redstone.setOutput("back", false)
end
if input == "2" then
for x = 1, 2 do
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
end
if input == "3" then
for z = 1, 3 do
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
end
if input == "4" then
for f = 1, 4 do
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
end
else print ("Invalid Number")
end
if x == "2" then
print ("Type Start to begin the mob arena")
end
elseif z == "3" then
print ("Type Start to begin the mob arena")
end
elseif f == "4" then
print ("Type Start to begin the mob arena")
end
elseif k == "1" then
print ("Type Start to begin the mob arena")
end

Thats my new code btw =P
Cranium #7
Posted 09 August 2012 - 09:50 PM
Print does not exist. perhaps you mean print? Lua is case sensitive, and almost all of the commands start with lowercase letters.
Silent_Potato #8
Posted 10 August 2012 - 03:08 AM
So, the code NOW has no errors; but now has the problem that when i type 1,2,3 or 4, it won't work. Help? D=
print ("Welcome to the Mob Arena!")
print ("Please type howmany players you have! [Up to a maximum of 4]")
sleep(10)
while true do
term.clear()
term.setCursorPos(1, 1)
input = io.read()
end
if input == "1" then
for k = 1, 1 do
redstone.setOutput("back", true)
sleep(1)
redstone.setOutput("back", false)
end
if input == "2" then
for x = 1, 2 do
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
end
if input == "3" then
for z = 1, 3 do
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
end
if input == "4" then
for f = 1, 4 do
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
sleep(0.5)
end
else print ("Invalid Number")
end
if x == "2" then
print ("Type Start to begin the mob arena")
end
elseif z == "3" then
print ("Type Start to begin the mob arena")
end
elseif f == "4" then
print ("Type Start to begin the mob arena")
end
elseif k == "1" then
print ("Type Start to begin the mob arena")
end