I'm using ComputerCraft, a minecraft mod that adds computers, modems, and other peripherals. In this mod, you can write code using Lua - lua

This is my first time using Lua, and with following a tutorial, Im making a chat program, allowing two players to send and receive messages to each other. The receiving is called chatreceive.lua. The script is:
rednet.open("right")
sender, message = rednet.receive()
print("computer " .. sender .. " has sent :")
print(message)
I keep getting an error saying
chatreceive.lua:3: attempt to concatenate global 'sender' (a nil value)
Any solutions? Ive been trying for a while now

That means that the variable 'sender' was nil. This probably means that there was some sort of error with rednet. You should add a lot of checks to make sure your programs don't fail in critical situations:
if sender==nil then
print("SENDER IS NIL!!!")
end
Also, what version of CC are you using? You shoud be more specific, to get better answers...

Related

I am getting the following errors with my Garry's Mod server

I've got an addon on my server that basically allows you to create territories. On top of that, I have one which allows you to make permanent properties, which players own even when they aren't online. Additionally, you are able to save the props inside these permanent buildings so that when you next get on, the props are all still there.
It was working fine, but I now seem to be encountering the following error whenever I save the props inside my house and restart the server. Also, a lot of the houses don't seem to work. But, when I unsave the props inside a house and restart, everything is back to normal.
The error
[ERROR] addons/darkrpmodification-master/lua/darkrp_modules/territory/sh_init.lua:514: bad argument #1 to 'pairs' (table expected, got nil)
1. pairs - [C]:-1
2. LoadProps - addons/darkrpmodification-master/lua/darkrp_modules/territory/sh_init.lua:514
3. tsetUpDoors - addons/darkrpmodification-master/lua/darkrp_modules/territory/sv_init.lua:273
4. unknown - addons/darkrpmodification-master/lua/darkrp_modules/territory/sv_init.lua:290
Code
function BuyableTerritory:LoadProps(steamid, t)
for k, v in pairs(t) do
local e = ents.Create("prop_physics")
e:SetPos(v.pos)
e:SetAngles(v.ang)
e:SetModel(v.model)
if v.color then
e:SetColor(v.color)
end
if v.material then
e:SetMaterial(v.material)
end
e:Spawn()
e.permaOwner = steamid
e:GetPhysicsObject():EnableMotion(false)
end
local ply = DarkRP.findPlayer(steamid)
if IsValid(ply) then
self:SetPropsOwner(ply, ply:SteamID())
end
The code starts at line 513, so the second line in is the one having problems. Thank you.
Just read the error message. It tells you that the input to pairs() is nil instead of the expected table. pairs is a so called iterator. It only works with a Lua table as input.
Your input t to BuyableTerritory:LoadProps(steamid, t) is not valid.
You either have to change that or check if t is a table befor you call pairs(t) to prevent the error from happening.
Go to line 273 of sv_init.lua to find out whats going on.

Lua script pc to sleep

So i got a new keyboard wit hG-keys. (hotkeys) And i'm not familiar with lua...
So could anybody give me a very simple command that sets my pc to sleep? please?
if gkey == 7 and mkey == 1 then
if event == "G_PRESSED" then
end
end
gkeys
so gkey is the key that is pressed, and mkey is the set it uses. i can have up to 54 differint scripts/macro's.
I want to know what i have to put after the last 'then' so my pc goes to sleep.
thx ahead
edit 1:
got this:
if gkey == 1 and mkey == 3 then
if event == "G_PRESSED" then
execute("rundll32.exe powrprof.dll,SetSuspendState 0,1,0");
end
end
error is: [string "LuaVM"]:40: attempt to call global 'execute'(a nil value)
and with os.execute i get this error:
[string "LuaVM"]:40: attempt to index global 'os'(a nil value)
final answer: not possible with gseries keyboard. use a shortcut
Given the reference to G-keys and the G_PRESSED in your code snippet, I am assuming you have one of the Logitech G-Series keyboards. These keyboards can be programmed so that certain keys run Lua scripts using a custom Lua 5.1 interpreter. The documentation is included with Logitech's Gaming Software program.
According to the documentation, only some of the standard library functions are supported: the string, math and table are available. However, the io, os and debug libraries are not available.
So I doubt you'll be able to make your PC go to sleep.
EDIT in response to OP edit: the Lua library you have access to has the os library removed, so you're probably not going to be able to make your computer sleep directly.
There might be an indirect way to do this by writing something that listens for debugging messages, which you can generate with OutputDebugMessage. There's a Team Speak plugin that does this. But it's probably beyond your programming ability right now and far beyond the scope of a Stackoverflow post to explain.
You can use os.execute to run an executable from Lua.
If you google "Windows sleep command line" you'll get another Stackoverflow post which shows two ways of doing it. One requires that you turn hibernation off, the other requires that you download an additional utility (PsShutdown).
Assuming you've downloaded PsShutdown and put it somewhere in your PATH, then you can use the following to sleep the computer:
os.execute('psshutdown -d -t 0')

Computer Craft modem.open( ) command giving me "attempt to index ? (a nil value)"

I am new to Computer Craft, and I want to get started using Wireless Modems.
local modem = peripheral.wrap("top")
local channel = 1
modem.open(channel)
I have the code above, and when I try to run the modem.open() command, it gives me a message saying "attempt to call index ? (a nil value)". I know that many people have had this error before. I have searched through questions and tutorials and followed their exact code or instructions, but none of them have worked and I cannot find any other questions with this error message showing up from this command. My modem is on top of my computer.
How can this be fixed?
If you are using wireless modems, considering using the rednet API.
rednet.open("top")
Configures your computer for sending messages wirelessly. Go to
the computercraft wiki for the full documentation
You can send messages using rednet.send() or rednet.broadcast() and receive messages through sender, message = rednet.receive()

Lua error for WoW addon (Tukui)

I don't have a lot of coding experience, did some C a few years ago, so that helps, but Lua handles things a bit differently, so I can't keep track.
I sometimes (not always) get this error when a friend or guildy logs into the game:
Date: 2013-06-14 16:57:57
ID: -1
Error occured in: Global
Count: 4
Message: ..\AddOns\Tukui\scripts\chat.lua line 335:
attempt to concatenate upvalue 'classColor' (a nil value)
Debug:
[C]: ?
Tukui\scripts\chat.lua:335: AddMessage()
..\FrameXML\ChatFrame.lua:2755: ChatFrame_MessageEventHandler()
..\FrameXML\ChatFrame.lua:2491: ChatFrame_OnEvent()
...s\WIM\Libs\LibChatHandler-1.0\LibChatHandler-1.0.lua:281:
...s\WIM\Libs\LibChatHandler-1.0\LibChatHandler-1.0.lua:252
...s\WIM\Libs\LibChatHandler-1.0\LibChatHandler-1.0.lua:308:
...s\WIM\Libs\LibChatHandler-1.0\LibChatHandler-1.0.lua:296
I have to do a reload of the ui after this happens to be able to see chat text again for that person.
Line 335 in that .lua file is this:
text = replace(text, "^|Hplayer:(.+)|h%[(.+)%]|h", "|Hplayer:%1|h|cff"..classColor.."%2|r|h")
Now I've learned that the .. indicates the concatenate function, but that isn't really helping me.
I don't know if this is enough information, but if you need it I can post the whole local function or whatever else is required.
If it makes any difference, I'm running the 3.3.5a WoW client.
You are probably using a global that gets defined from some other addon in a now deterministic way
While the (classColor or "") will get you rid of the error, you should try and find why that variable (classColor) is sometimes defined and sometimes not. Maybe it happens only for certain classes?
A simple hack would be to just replace
..classColor..
with
..(classColor or "")..
where it will select a blank string when classColor has no value assigned to it.

Erlang Bit Syntax pattern matching works in shell but not as passed argument

I am trying to make a simple UDP packet decoder.
packet_decoder(Packet)->
<<Opts:8,MobIdLength:8,MobId:64,MobIdType:8,MgeType:8,SeqNum:16,Rest/binary>> = Packet,
io:format("Options:~p~n",Opts),
io:format("MobIdLength:~p~n",MobIdLength),
io:format("MobId:~p~n",MobId),
io:format("MobIdType:~p~n",MobIdType),
io:format("MgeType:~p~n",MgeType),
io:format("SeqNum:~p~n",SeqNum).
Packet is passed by a receive loop:
rcv_loop(Socket) ->
inet:setopts(Socket, [{active, once}, binary]),
io:format("rcvr started:~n"),
receive
{udp, Socket, Host, Port, Bin} ->
packet_decoder(Bin),
rcv_loop(Socket)
end.
I keep getting(following error edited 9/7/12 9:30 EST):
** exception error: no match of right hand side value
<<131,8,53,134,150,4,149,0,80,15,1,2,1,2,0,16,80,71,115,
52,80,71,115,53,24,63,227,197,211,...>>
in function udp_server:packet_decoder/1
called as udp_server:packet_decoder(<<131,8,53,134,150,4,149,0,80,15,
1,2,1,2,0,16,80,71,115,52,80,71,
115,53,24,63,227,197,...>>)
in call from udp_server:rcv_loop/1
in call from udp_server:init/0
If I create the same variable in the Erlang shell as a binary, i.e.
Packet = <<131,8,53,134,150,4,149,0,80,15,1,2,1,2,0,16,80,71,115,52,80,71,115,53,24,63,227,197,211,228,89,72,0,0,0,0,0,0,0,16,0,5,5,32,1,4,255,159,15,18,28,0,34,62,2,0,0,0,0,0,0,0,47,67>>.
<<Opts:8,MobIdLength:8,MobId:64,MobIdType:8,MgeType:8,SeqNum:16,Rest/binary>> = Packet.
It works just fine. Is there some subtlety in passing this to a function that I am missing? I have tried what I think is everything(except the right way). I tried setting the type and size. I also just tried
<<Rest/binary>> = Packet.
To no avail. Any help much appreciated.
The error you are getting when you run your code does not match your code. The error you are getting:
** exception error: no match of right hand side value ...
is a badmatch error and comes from an explicit = match where the pattern does not match the value from the RHS. There is no = in the code for rcv_loop/1. This implies that the loop you are running is not this code. So there are some questions to be asked:
When you have recompiled the module containing rcv_loop/1 have you restarted the loop so you run the new code? This is not done automagically.
Are you sure you are loading/running the code you think you are? I know this question sounds stupid but it is very easy, and not uncommon, to work on one version of the code and load another. You need to get the paths right.
The other things about mentioned about your code would not give this error. The calls to io:format/2 are wrong but would result in errors when you make the actual calls to io:format/2. Using the variable Socket as you do is not an error, it just means that you only want to receive UDP packets from just that socket.
EDIT : the first part of my answer was completely wrong so in order to not mislead, I deleted it.
Like spotted Alexey Kachayev io:format takes as second parameter a list, so :
packet_decoder(Packet)->
<<Opts:8,MobIdLength:8,MobId:64,MobIdType:8,MgeType:8,SeqNum:16,Rest/binary>> = Packet,
io:format("Options:~p~n",[Opts]),
io:format("MobIdLength:~p~n",[MobIdLength]),
io:format("MobId:~p~n",[MobId]),
io:format("MobIdType:~p~n",[MobIdType]),
io:format("MgeType:~p~n",[MgeType]),
io:format("SeqNum:~p~n",[SeqNum]).
I figured it out(kinda). I had been working on this in erlide in eclipse which had worked fine for all of the other parts of the. I tried compiling it from the erl shell and it worked fine. There must be some minor difference in the way eclipse is representing the source or the way it invokes the erlang compiler and shell. I will take it up with erlide.org. Thanks for the help!

Resources