9 posts
Posted 11 June 2013 - 10:08 PM
Hi
I am trying to create a program using the player detector from misc peripherals. The program is supposed to get the names of the player, insert that name into the table and then print the table to the monitor.
When i run the code i get detector:1: attempt to call nil
I am new to programming and iv only just started using tables. All help is appreciated.
Here is the pastebin link
http://pastebin.com/PL4UBRcCThankyou in advance
Beechey
8543 posts
Posted 12 June 2013 - 02:23 AM
Split into new topic.
758 posts
Location
Budapest, Hungary
Posted 12 June 2013 - 02:31 AM
You have to set Side before using it. By the way I think that the error is on line 3 - there is no actual code on line 1.
AND You have to use a = on line 5 before {}.
AND table.insert needs only two parameters - the table and the value - it will insert the value to the end of the table.
AND on line 11, player should be in quotations, since it's a string.
AND on line 13, you get the Num-th entry of NameCollection using NameCollection[Num].
AND after table.insert you have update Num using Num = #NameCollection - it won't update itself.
7508 posts
Location
Australia
Posted 12 June 2013 - 02:39 AM
Some notes with your code
mon = peripheral.wrap(Side) --# you need to specify a side as a string here
Side = mon.getSide() --# there is no getSide function on the mon peripheral
NameCollection {} --# you're missing an = here
Num = #NameCollection
mon.write("Touch detector to input name.")
while true do
event, playerName = os.pullEvent()
if event == player then --# player is not a valid variable, you're missing the quotes to make it check if it is a player event
table.insert(NameCollection, Num + 1, playerName) --# this will always insert in the same index, not at the end
mon.write(NameCollection, Num +1) --# we cannot print a table in this way, also if this did work it would print the same name each time as you never increment Num
end
end
this is how the program should be done
local mon = peripheral.wrap("left")
local nameCollection = {}
mon.write( "Touch detector to input name." )
while true do
local event, playerName = os.pullEvent()
if event == "player" then
table.insert(nameCollection, playerName)
-- or this
-- nameCollection[#nameCollection + 1] = playerName
mon.write( "Added "..playerName )
-- or as I think you were meaning
mon.write( "Added "..nameCollection[#nameCollection] )
end
end
EDIT: Damn LBPHacker posting then editing… stop trying to make yourself look like a ninja. :P/>
Edited on 12 June 2013 - 12:40 AM
758 posts
Location
Budapest, Hungary
Posted 12 June 2013 - 02:46 AM
EDIT: Damn LBPHacker posting then editing… stop trying to make yourself look like a ninja. :P/>
Haven't updated the page - didn't notice you… (And of course IP.Board didn't notifiy me). Edited the crap out of my comment after re-reading the pastebin.
9 posts
Posted 12 June 2013 - 05:50 AM
Thanks guys, just updated the code and it is to working. I want it to write a list of names that it collects though, so if i click it my name will be on the screen and then if say you click it your name will be there underneith. Is that possible?
758 posts
Location
Budapest, Hungary
Posted 12 June 2013 - 08:43 AM
Bit showed you the more difficult part of that, I'll just wrap up what's left.
local mon = peripheral.wrap("left")
local nameCollection = {}
mon.clear()
mon.setCursorPos(1, 1)
mon.write("Touch detector to input name")
local offset = 1 -- in case you removed the text printed above, set this to 0
local width, height = mon.getSize()
while true do
local event, playerName = os.pullEvent()
if event == "player" then
table.insert(nameCollection, playerName)
mon.setCursorPos(1, #nameCollection + 1)
for i = 1, math.min(#nameCollection, height - offset) do
mon.setCursorPos(1, i + offset)
mon.clearLine()
mon.write("Added " .. nameCollection[i + math.max(#nameCollection - height + offset, 0)])
end
end
end
9 posts
Posted 12 June 2013 - 09:28 AM
Ok awesome Thankyou for the help. Il do that when I get home from work tonight. :)/>