lua attempt to call field 'createUDPSocket' (a nil value) - lua

I am writing a LUA script, when I run the program, this result as below:
socket = net.createUDPSocket()
attempt to call field 'createUDPSocket' (a nil value)
I have been searching for many website, there seems to no much details about that. Could anyone help?

That simply means that the createUDPSocket function does not exist. Without seeing your code, that is the most anyone here is going to be able to help you with.

Related

My lua code produces the error "Object tried to call nil" when trying to check if a player has a certain trait [PZ]

I'm attempting to mod a game I've been playing recently, and I've encountered an error when writing some code that adds a new mechanic. The problem itself isn't very complicated, but I can't seem to figure out what the solution is in this context. I'm pretty new to coding, especially with lua, so I apologize if the solution is really obvious. I've looked around for some answers, but again, nothing I can find seems to help me in this specific context. The game I'm trying to mod is called Project Zomboid, if that helps.
When attempting to start the game with the mod enabled, the error "Object tried to call nil in isBloodthirsty" pops up. Here's the snippet of code that's causing the error:
local function isBloodthirsty(player)
if player:getDescriptor():getTrait() ~= "bloodthirsty" then
return false else
return true
end
end
Ignoring how poorly written it is, the code was supposed to check if the player had a certain trait, and if so, then set the value isBloodthirsty to true. I think the error is caused by lua not recognizing the value "bloodthirsty", but I'm not sure what I should put instead. If anybody has an idea of what I'm doing wrong, I'd greatly appreciate some help. If it's helpful, I can post the rest of the code.
Thanks to all the great help from the stack overflow community, I managed to figure out what my problem was. The code I had written wasn't working because lua didn't recognize "bloodthirsty" as a valid trait string. My solution was to mix up the code a bit and frame the trait as a profession instead (a profession is kind of like a collection of traits within the game). The following code worked:
local function bloodthirstyStart(player)
if player:getDescriptor():getProfession() ~= "bloodthirsty" then
return
end

Trying to fix an auto farming script for a mobile game

I found a 5 month old lua script to automatically farm currency and items in a mobile game called Soda Dungeon, the script was finicky when I found it and wouldn't work properly so I'm trying to fix it with very little lua experience.
Here is the code Ive edited: http://pastebin.com/U9Ymej0z
it runs fine until it tries to start the dungeon and I get this error:
Runtime error: com.appautomatic.ankulua.f:
Can't find dungeon_level_up.png
stack traceback:
[C]: in function 'continueClick'
?: in function <?:291>
(tail call):?
/storage/extSdCard/soda_dungeon/main.lua:
217: in function 'main'
/storage/extSdCard/soda_dungeon/main.lua:
239: in main chunk
Any help is greatly appreciated.
I don't know anything about Ankulua but a quick session with our best buddy google gave me this:
http://ankulua.boards.net/thread/13/advanced-methods
http://ankulua.boards.net/thread/6/objects-methods-introduction-sikuli-compatible
They give those functions:
continueClick(x, y, xRandom, yRandom, times)
continueClick(PSMRL, times [,timeout])
They say continueClick will click on a position given by x,y or PSMRL, which will resolve in a postion as well.
You enter a string that contains an image name.
I assume the message
Can't find dungeon_level_up.png
Is rather an exception text of some find(PS) function inside Ankulua than a problem with finding an image file. Replace that image name with some coordinates and check what is happening.
I would not trust that function call in your script as the author of that script added a comment that he does not know what continueClick returns. So maybe he did not know how to use it as well.
The documentation says:
There is no return value.
Btw, the author of Ankulua offered support via mail. So why don't you ask him? I'm sure he'll be of better help than anyone here.
let me offer you another alternative method, just a few simple click you and drag
It doesn't require root,
background service Installation can be found at the following
hXXp://123autoit.blogspot.tw/2016/08/123autoit-non-root-daemon-service.html
currently only works on ARM
for Android 5.0+
hXXp://play.google.com/store/apps/details?id=com.autoit.nonroot&hl=en
for Android 4.2~4.4
hXXps://play.google.com/store/apps/details?id=com.autoit.nonroot.legacy&hl=en
feel free to send me message to ask for question
here is what it can do
check out the blog, it contains tutorials, and video demo
http://123autoit.blogspot.tw/
Hope everything works out for you,
cheers

Lua table.insert not working

So I just want to insert a value into a table I defined, but it doesn't seem to be working at all.
If I use this code:
t = {1,2,3}
table.insert(t, 9)
all i get is
LuaInterface.LuaScriptException: [string "main"]:2: attempt to call
field 'insert' (a nil value)
Is it possible that I'm using a compiler that just doesn't have this function? I really don't get it. I'm using the BizHawk Emulator for compiling right now if that matters.
Does by chance get "table" or "table.insert" get overwritten somewhere previously in the code? Or maybe the table library was not loaded?
Try
require("table")
prior to the call to table.insert and see if it works then

Lua: attempt to index field '?' (a nil value)

I am just starting with lua, and i have some code, from someone with some intentionally mistakes in it. Now i've hit a roadblock getting this error on and on for the following code:
function SIM_Utils:ClickButton(app, buttonName)
page = app:getTopPage()
widgets = page:getWidgets(buttonName)
print (type(widgets))
print (widgets[1])
widgets[1]:click(true, 5000)--this yields "attempt to index field '?' (a nil value)"
widgets[1]:click(false,0)--this yields "attempt to index field '?' (a nil value)"
app:captureScreen()
end
This will result in:
table
WidgetCommon (09590790)
L.E.
After running what Alex posted here is the result:
widgets=
table: 0A45CF28
1
WidgetCommon (09590790)
Is table: 0A... the answer i am looking for?
L.E. 2: Reposted the whole function since it seems this is where the problem lays
What is page:getWidgets returning? You can check it with print(type(widgets)). If it is a table, then array position 1 is not defined in that table (you can loop through the table contents using the pairs function). If it's not a table, then you're attempting to lookup an index on something that isn't a table, which won't work.
Also, since you're new to Lua, realize that page:getWidgets is not a built-in component. So, you'll need to load this functionality or use the appropriate derived application that provides this function.
Try running the following code:
widgets = page:getWidgets(buttonName)
print("widgets=", widgets) -- Should print out something like "Widgets= 0x12345678". If it doesn't then widgets is nil. In other words nothing is there.
for k, v in pairs(widgets) do
print(k, v)
end
That will make sure you make sure you have a widgets table, and if you do, it'll tell you whats in it. If you don't have anything at an index of 1, then that's your problem: page:getWidgets(buttonName) is not returning a list of widgets like you expect.
It seems unlikely, but it's possible (and it in fact looks like the only possibility from the info you've posted) that the library you're using is throwing the error, not your code. Be aware that when an error is thrown in Lua, the throwing code and tell Lua to show the error as thrown from your calling code, rather than in the Library code. That should only be used for logical errors like "Bad arguments to Widget::show", not something like what you're getting, but it can happen. Also, an error in a C function will show up sort of like it came from your code. EG:
io.open("myFile").read() --> should be ":read", not ".read"
stdin:1: attempt to index a nil value
stack traceback:
stdin:1: in main chunk
[C]: ?
Do you have a stack traceback to show as well as the error?

How to validate a pattern in lua

I'm currently creating a search function in lua which basically just goes through a list of items and processes the items that match the input string in a specific way.
I use string.find(sourceString, inputString) to identify the items.
The function is called whenever a user types something into a text field so if he tries to enter a pattern it happens that when using sets or captures the function is called when the search string just contains a [ or a ( without the closing equivalent which of cause throws an error.
The best way to go around this problem I think is to validate the input as beeing a valid pattern, but I've got no idea how to do this. Lua itself doesn't seem to contain a method for this and I am a bit confused of how to check it in a more or less performant way myself. Thanks for your help and ideas in advance :)
You should wrap the call to string.find with pcall to capture the error.
local status, result = pcall(string.find, sourceString, inputString)
if not status then
-- bad pattern logic, error message is in result
else
-- good pattern logic, result contains the start index
end
See this for pattern escape function (taken from somewhere in Lua Users wiki, I think). You may convert it to the validation function if you need.

Resources