term.write("How many Players: ")
local p = read()
for i = 1, p do
term.write("Player Name: ")
read()
end
local p = tonumber(read())
term.write("How many Players: ")
local p = tonumber(read())
local players = {} --# Create the table.
for i=1,p do
term.write("Player Name: ")
players[#players + 1] = read() --# Get input and insert the input into the table.
end
...
term.write("Player Name: ")
players[read()] = 0 --# Insert the player's name into the table and assign a value of 0 to it.
...
players["James"] = 8 --# Set James's score to 8.
print(players["James"]) --# Print James's score.
Your code won't work, because "p" is a string. So before looping you would need to convert it to a number:local p = tonumber(read())
Anyway, you should insert all of the entered names into a table:term.write("How many Players: ") local p = tonumber(read()) local players = {} --# Create the table. for i=1,p do term.write("Player Name: ") players[#players + 1] = read() --# Get input and insert the input into the table. end
EDIT:
Since you're tracking scores, instead of inserting the name into the table like above, you should use the name as a key for the table.
E.g:... term.write("Player Name: ") players[read()] = 0 --# Insert the player's name into the table and assign a value of 0 to it. ...
Now you can use the table like this:players["James"] = 8 --# Set James's score to 8. print(players["James"]) --# Print James's score.
Is the code already importing the names into a table or do I have to add that code?Anything after a – is a comment, and Lua will ignore it.
If using the names as table keys, then you don't need to include them in the code of your script. In Lignum's example, it'll ask for them when you run it, and put them into the table "on the fly".
If not referring into tables, then you would need to put the names directly into your code.
term.write("How many Players: ")
local p = tonumber(read())
local players = {} --# Create the table.
for i=1,p do
term.write("Player Name: ")
players[read()] = 0 --# Insert the player's name into the table and assign a value of 0 to it.
end
Now you can use the table like this:players["James"] = 8 --# Set James's score to 8. print(players["James"]) --# Print James's score.
term.write("How many Players: ")
local p = tonumber(read())
local players = {} --# Create the table.
for i=1,p do
term.write("Player Name: ")
players[#players+1] = {["name"] = read(), ["score"] = 0}
end
-- Then you can do stuff like this:
for i=1,#players do
print("Player "..tostring(i).." is called "..players[i].name.." and has a score of "..players[i].score..".")
end
term.write("How many Players: ")
local p = tonumber(read())
if p == nil then
print("Not a number!")
sleep(1)
os.reboot()
end
it is never a good idea to reboot a computer for something as simple as what a loop could replaceYou can also check if what they inputed is actually a number.term.write("How many Players: ") local p = tonumber(read()) if p == nil then print("Not a number!") sleep(1) os.reboot() end
--# forward declaration of `p` to make it local but still usable outside the loop
local p
--# loop
repeat
write("How many players: ")
p = tonumber(read())
--# if it wasn't a number
if not p then
print("Not a number!")
end
--# until `tonumber`ing the input is a valid number
until p
print("Playing with "..p.." players")
Or use:You can also check if what they inputed is actually a number.term.write("How many Players: ") local p = tonumber(read()) if p == nil then print("Not a number!") sleep(1) os.reboot() end
type(p)
there is no point, the type of `p` will either return as "number" or "nil" meaning that a check like soOr use:type(p)
if p then
--# code
end
would adequately test that the input was a number, assuming of course that `p` is defined like so
local p = tonumber(read())
it is never a good idea to reboot a computer for something as simple as what a loop could replaceYou can also check if what they inputed is actually a number.term.write("How many Players: ") local p = tonumber(read()) if p == nil then print("Not a number!") sleep(1) os.reboot() end
--# forward declaration of `p` to make it local but still usable outside the loop local p --# loop repeat write("How many players: ") p = tonumber(read()) --# if it wasn't a number if not p then print("Not a number!") end --# until `tonumber`ing the input is a valid number until p print("Playing with "..p.." players")
We'll yeah, you would put other code, but for example purposes, I put os.reboot().
We'll yeah, you would put other code, but for example purposes, I put os.reboot().
That's not a good coding practice. Firstly, it's horribly inefficient. It takes several seconds for a whole reboot, whereas a loop allows the code to run as fast as Minecraft would allow it. Second, it's completely impractical and unrealistic in the real world. Would you EVER use a program which repeatedly rebooted your computer just to achieve a simple loop? I doubt it.
So yeah, don't do it. And please don't teach others to do it either.
Could I put the names that the player entered into a table so that a could reference later what player I am talking to?We'll yeah, you would put other code, but for example purposes, I put os.reboot().
That's not a good coding practice. Firstly, it's horribly inefficient. It takes several seconds for a whole reboot, whereas a loop allows the code to run as fast as Minecraft would allow it. Second, it's completely impractical and unrealistic in the real world. Would you EVER use a program which repeatedly rebooted your computer just to achieve a simple loop? I doubt it.
So yeah, don't do it. And please don't teach others to do it either.
Ok, whatever. Won't do it again.