Attempt to index a boolean value error when calling this function - lua

I get the attempt to index a boolean value error when calling this function:
M.on_attach = function(client, bufnr)
if client.name == "tsserver" then
client.resolved_capabilities.document_formatting = false
end
lsp_keymaps(bufnr)
lsp_highlight_document(client)
end
Can anyone help me?
Edit:
I get the error on the second line here:
local opts = {
on_attach = require("s3m.lsp.handlers").on_attach,
capabilities = require("s3m.lsp.handlers").capabilities,
}

require("s3m.lsp.handlers") returns true which is a boolean value. with require("s3m.lsp.handlers").on_attach you index that nil value which is an invalid operation in Lua.
For this to make sense your required script must return a value. In your case M
require returns true if the script is loaded successfully but does not return a non-nil value. Please read the manual to avoid such errors.

Related

Can anyone tell me that the error "calling xxxx on bad self" shows

I use lua in Unity. When I call the function **TryDisableHeadWear **, there are some error I can't understand.
calling 'GetSurfaceName' on bad self (string expected, got nil)
function def:TryDisableHeadWear()
if not self:CheckSurfaceName(self._hat_go) then
self._hat_go:SetActive(false)
end
end
function def:CheckSurfaceName(obj)
local surface_name = self:GetSurfaceName(obj)
return head_wear_surface[surface_name] ~= nil
end
local reg = "(%w+)"
function def:GetSurfaceName(obj)
if not obj then return end
local root_obj = self:GetSurfaceObj(obj)
local surface_obj_name = root_obj and root_obj.name
return surface_obj_name and string.match(surface_obj_name, reg)
end
function def:GetSurfaceObj(obj)
if string.find(obj.name, "Surface") then
return obj
else
local parent = obj.transform.parent
if not parent then return end
return self:GetSurfaceObj(parent.gameObject)
end
end
Can anyone tell me what the error means?
I try to call the function in normal environment. I get no error and everything goes well. But where are some error report in firebase that means the code above has bugs

LUA using function variable outside of it

I am trying to edit a script in LUA but I couldn't get access of a local defined in a function
LUA Code
function getSafeMoney()
local SafeMoney = nil
QBCore.Functions.ExecuteSql(false, 'SELECT * FROM `moneysafes` WHERE `safe` = "mechanic"', function(result)
SafeMoney = json.decode(json.encode(result[1])).money;
end)
return SafeMoney
end
print(getSafeMoney())
Result :
nil
here's the sql function as sysdevs asked
QBCore.Functions.ExecuteSql = function(wait, query, cb)
local rtndata = {}
local waiting = true
exports['ghmattimysql']:execute(query, {}, function(data)
if cb ~= nil and wait == false then
cb(data)
end
rtndata = data
waiting = false
end)
if wait then
while waiting do
Citizen.Wait(5)
end
if cb ~= nil and wait == true then
cb(rtndata)
end
end
return rtndata
end
Your second function is not holding the main function I assume, I am bad at lua aswell, but I think that changing
QBCore.Functions.ExecuteSql(false, ............
to
QBCore.Functions.ExecuteSql(true, ............
This will probably fix your problem, still your code is ambiguous and needs concentration if you could provide more information I might be able to help more

Lua : return table trough function is nil

I get the error: source_file.lua:5: attempt to call a nil value (global 'getCard')
i try to catch the right table in questCards which Index=name is the same as the given string from objName
questCards={{['name']='test1',['creatureName']='test3'},{['name']='test2',['creatureName']='test4'}}
obj='test1'
card=getCard(obj)
card['creatureName']=nil --Only for test purpose
if card['creatureName']==nil then
--do Somthing
end
function getCard(objName)
for k,v in pairs(questCards) do
if v['name']==objName then
return v
end
end
end
The error message is telling you that getCard is not defined at the point where it is called.
You need to define getCard before calling it.

LUA Error: attempt to index a nil value #6:16

I am trying to retrieve the Entryhandle UUID by sending a request. However, I am getting this error every time. Can anyone help me solve it or point out where I am making a mistake?
local config={}
config.mcast_mac = "00:0a:cd:16:da:f1"
function rpc:epm()
local pkt = CreateFromPath("ethernet/ip/udp/dcerpc/epm")
--[[data is put here]]
SendAndWait(pkt, function(res)
local epm = res.get_layer("epm")
--[[data is put here--]]
handle = epm.EntryHandleUUID.to_string()
print("EntryHandleUUID:",handle)
end
end,2000)
return handle
end
This code not valid Lua code. Suppose remove end from line after print.
To findout where you try get access to nil value you can just add assert before each index operation.
local config={}
config.mcast_mac = "00:0a:cd:16:da:f1"
assert(rpc, 'rpc is NULL')
function rpc:epm()
local pkt = CreateFromPath("ethernet/ip/udp/dcerpc/epm")
--[[data is put here]]
SendAndWait(pkt, function(res)
assert(res, 'res is NULL')
local epm = res.get_layer("epm")
--[[data is put here--]]
assert(epm, 'epm is NULL')
local uuid = epm.EntryHandleUUID
assert(uuid, 'epm.EntryHandleUUID is NULL')
handle = uuid.to_string()
print("EntryHandleUUID:",handle)
end, 2000)
return handle
end

Returning a class in Lua

I'm trying to create a plugin but I can't seem to access the returned player class from outside the GetPlayer() function.
This is the GetPlayer Fuction:
function GetPlayer(Player_To_Find) -- This is the function we use to verify the user exists, It will return the user class if the user exists
LOG("Finding " .. Player_To_Find) --False if they do not exist
local Found = false
local FindPlayer = function(TargetPlayer)
if (TargetPlayer:GetName() == Player_To_Find) then
Found = true
print("Found " .. TargetPlayer:GetName())
return TargetPlayer
end
end
cRoot:Get():FindAndDoWithPlayer(Player_To_Find, FindPlayer)
if Found == true then return TargetPlayer else return false end
end
If I try to call the TargetPlayer class after it has returned using this snippet:
TargetPlayer=GetPlayer(Target)
if TargetPlayer ~= false then
LOG(TargetPlayer:GetName())
It will fail with the error:
attempt to index global 'TargetPlayer' (a nil value)
Can anyone point me in the right direction, It's taken me a long time and I've come up blank.
The parameter TargetPlayer is only in scope in the function body. The TargetPlayer in the last line of GetPlayer refers to a global variable, which is presumably nonexistent and therefore nil.
Declare a local variable in the GetPlayer function, set it in the body of the FindPlayer function, and return it at the end of the of GetPlayer (also don't return false if a player can't be found, return nil, which semantically means "nothing").

Resources