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
Related
I'm trying, to wrap my head around Touchosc and script based on LUA 5.1.
I have a number of labels, called song0, song1, song2, and so on. I'm trying to set different values in these, using
local text = 'Smoke On The Water'
for i = 1, 2 do
self.children.pager1.children.main.children.song[i].values.text = text
end
but that gives me an error.
:-) I do need help.
Finn
Since you haven't provided the actual error, it's difficult to say what the problem is, but if I have to venture a guess, then try replacing ...children.song[i].values... with ...children["song"..i].values.... Since there is no table song, you just need to generate dynamic field names.
While I was inside of Roblox Studio, I was making a script like the one before you:
local amount = 123
local Module = require(game.ServerScriptService:WaitForChild("Module"))
script.Parent.ClickDetector.MouseClick:Connect(function(player)
if player.leaderstats.Currency.Value >= amount then
player.leaderstats.Currency.Value = player.leaderstats.Currency.Value - amount
local pet = Module.chooseRandomPet()
--> print(pet.Name.." selected") <--
end
end)
When I went to use it I was greeted with "attempt to concatenate nil with string" around the area with the arrows.
What would I do in order to fix this hindrance.
Module.chooseRandomPet() returns a table that has no field "Name". I'd guess that you get a simple Lua table where you expect to get a Roblox Instance. Or for some reason you managed to assign nil to that Instance's Name property.
Either way you should make find out why you don't what you expect. I found various petModule problems online. All of them had a lot of code in common that was full of mistakes.
If you cannot make sure that you don't get what you expect you should at least handle that case properly by checking if it's a nil value you're about to concatenate.
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.
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?
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.