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

HEEEEELP

Started by joshgreat, 12 July 2017 - 01:59 PM
joshgreat #1
Posted 12 July 2017 - 03:59 PM
ok. so im making an OS and im making a multi user account system, sadly this comes up with an error. if someone can find the error and tell me whats wrong that would be dandy. Thanks

LINK

https://pastebin.com/HH67n4YQ

p.s this is only a portion of the code
KingofGamesYami #2
Posted 12 July 2017 - 04:25 PM
That should not throw an error. Are you sure this is the right file and line number?

It won't work properly in any case, since fs.exists takes one parameter not two.
joshgreat #3
Posted 12 July 2017 - 04:48 PM
It has a problem adding strings as when i make it print the variable after 2 have been added it is wrong and only shows the 1st string
KingofGamesYami #4
Posted 12 July 2017 - 04:54 PM
"Adding strings" is called concatenation. The operator for this in Lua is two periods. Example:

print( "Hello " .. "World!" )
joshgreat #5
Posted 12 July 2017 - 05:17 PM
I have a problem with adding variables as well as it does the same
KingofGamesYami #6
Posted 12 July 2017 - 05:43 PM
That sentance makes me want to stop helping you.
joshgreat #7
Posted 12 July 2017 - 06:51 PM
sorry, its the same solution isnt it?
diamondpumpkin #8
Posted 12 July 2017 - 07:15 PM
sorry, its the same solution isnt it?

Sort of… Adding two variables is dependent on the variable type. So a string variable would work as a string just without quotes.


ex = "Hello"
ex1 = "world"
print(ex .. " " .. ex1)

Outputs:
Hello world

However, if the variable is a number use it as you would a number


print(5 + 5)
Outputs:
10

num = 5
num1 = 6

print(num + num1)
Outputs:
11

Mixing the two will not work. It will throw an error. If you want to a print a number in a string, easy.


n = 1
print("Hello, you are " .. tostring(n) .. " person.")
Outputs:
Hello, you are 1 person.

Or you could just change the original variable to a string:
tostring(n)

If you want to do the opposite, say in a math equation, you'll need to use tonumber().

n = "5"
tonumber(n)
if n == 5 then
  print(5)
end

This code is unoptimized because it was done quickly, however it's still a decent example.
Dog #9
Posted 12 July 2017 - 07:58 PM
One thing you missed, diamondpumpkin, is that you need to capture the output of tonumber() and tostring() to a variable. They don't change a variable/value, they output a new value.


local n = 1
local nString = tostring(n)
joshgreat #10
Posted 12 July 2017 - 08:33 PM
Thanks guys il edit my code :-D
Bomb Bloke #11
Posted 13 July 2017 - 01:09 AM
Mixing the two will not work. It will throw an error.

Actually, Lua will quite happily concatenate strings with numbers, or add string representations of numbers together:

print("2" + "2") --> 4

print("2" + 2) --> 4

print(2 .. 2) --> 22

print("2" .. 2) --> 22

That said, you can't pass a string into something like a "for" loop or a numeric comparison without doing something to convert its type first.