TwitchyCake
TwitchyCake

Reputation: 1

Lua - Couple Questions

Im a amatuer at coding. So, mind me if i face palmed some things.

Anyways, im making a alpha phase for a OS im making right? I'm making my installer. Two questions. Can i get a code off of pastebin then have my lua script download it? Two. I put the "print" part of the code in cmd. I get "Illegal characters". I dont know what went wrong. Here's my code.

 --Variables
Yes = True
No = False
--Loading Screen
print ("1")
sleep(0.5)
print("2")
sleep(0.5)
print("Dowloading OS")
sleep(2)
print("Done!")
sleep(0.2)
print("Would you like to open the OS?")
end

Upvotes: 0

Views: 144

Answers (2)

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23727

You can implement sleep in OS-independent (but CPU-intensive) way:

local function sleep(seconds)
  local t0 = os.clock()
  repeat
  until os.clock() - t0 >= seconds
end

Upvotes: 0

exists-forall
exists-forall

Reputation: 4258

I see a few issues with your code.

First of all, True and False are both meaningless names - which, unless you have assigned something to them earlier, are both equal to nil. Therefore, your Yes and No variables are both set to nil as well. This isn't because true and false don't exist in lua - they're just in lowercase: true and false. Creating Yes and No variables is redundant and hard to read - just use true and false directly.

Second of all, if you're using standard lua downloaded from their website, sleep is not a valid function (although it is in the Roblox version of Lua, or so I've heard). Like uppercase True and False, sleep is nil by default, so calling it won't work. Depending on what you're running this on, you'll want to use either os.execute("sleep " .. number_of_seconds) if you're on a mac, or os.execute("timeout /t " .. number_of_seconds) if you're on a PC. You might want to wrap these up into a function

function my_sleep_mac(number_of_seconds)
    os.execute("sleep " .. number_of_seconds)
end

function my_sleep_PC(number_of_seconds)
    os.execute("timeout /t " .. number_of_seconds)
end

As for the specific error you're experiencing, I think it's due to your end statement as the end of your program. end in lua doesn't do exactly what you think it does - it doesn't specify the end of the program. Lua can figure out where the program ends just by looking to see if there's any text left in the file. What it can't figure out without you saying it is where various sub-blocks of code end, IE the branches of if statements, functions, etc. For example, suppose you write the code

print("checking x...")
if x == 2 then
print("x is 2")
print("Isn't it awesome that x is 2?")
print("x was checked")

lua has no way of knowing whether or not that last statement, printing the x was checked, is supposed to only happen if x is 2 or always. Consequently, you need to explicitly say when various sections of code end, for which you use end. For a file, though, it's unnecessary and actually causes an error. Here's the if statement with an end introduced

print("checking x...")
if x == 2 then
print("x is 2")
print("isn't it awesome that x is 2?")
end
print("x was checked")

although lua doesn't care, it's a very good idea to indent these sections of code so that you can tell at a glance where it starts and ends:

print("checking x...")
if x == 2 then
    print("x is 2")
    print("isn't it awesome that x is 2?")
end
print("x was checked")

with regards to your "pastebin" problem, you're going to have to be more specific.

Upvotes: 3

Related Questions