• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua error: attempt to call global 'require' (a nil value)

monfik

New Member
Joined
Jun 28, 2023
Messages
6
Reaction score
2
GitHub
Munvik
Hello. I have a problem with lua script on my local server. I added a command for GameMaster in talkactions.xml.
And when I call a command in game I receive an error in console

Code:
Lua Script Error: [TalkAction Interface]
data/talkactions/scripts/eqSpawn.lua:onSay

data/talkactions/scripts/eqSpawn.lua:2: attempt to call global 'require' (a nil value)
stack traceback:
        data/talkactions/scripts/eqSpawn.lua:2: in function <data/talkactions/scripts/eqSpawn.lua:1>

My .lua code is simple
Lua:
function onSay(cid, words, param)
    local createitemscript = require("createitembyname")
end

It looks like I have no require method? But when I type require and try to open () it says for me what it does..
Can u help? Im new in lua programming

Server version: 0.6.4
 
Last edited:
Solution
Yea thas has a sense but what if I really want to use that function from createitembyname.lua to use that flow from this function? In function in this .lua file there is a flow checking if item exists and so on...
Lets say I want force to use that function onSay(cid, words, param) from createitembyname.lua in mine script?
You can use the function that has been declared in another file by executing the code in that file and saving the function in a local or global variable so that you can use it later

Lua:
dofile("data/talkactions/scripts/createitembyname.lua")

local myCustomFunc = onSay

function onSay(cid, words, param)
    return myCustomFunc(cid, words, param)
end

I don't understand why you would want to...
Hello. I have a problem with lua script on my local server. I added a command for GameMaster in talkactions.xml.
And when I call a command in game I receive an error in console

Code:
Lua Script Error: [TalkAction Interface]
data/talkactions/scripts/eqSpawn.lua:onSay

data/talkactions/scripts/eqSpawn.lua:2: attempt to call global 'require' (a nil value)
stack traceback:
        data/talkactions/scripts/eqSpawn.lua:2: in function <data/talkactions/scripts/eqSpawn.lua:1>

My .lua code is simple
Lua:
function onSay(cid, words, param)
    local createitemscript = require("createitembyname")
end

It looks like I have no require method? But when I type require and try to open () it says for me what it does..
Can u help? Im new in lua programming

what u want to do?
u do have require function, but u are not using it properly..
 
what u want to do?
u do have require function, but u are not using it properly..
I want to use createitembyname.lua script inside mine and use onSay(...) function in it.

Originally I had the script like this:
Lua:
function onSay(cid, words, param)
    local createitemscript = require("createitembyname")
    createitemscript.onSay(cid, words, param)
end

Sorry if u need to introduce/help me into lua.
I saw call of "require" like this on stackoverflow.
 
I want to use createitembyname.lua script inside mine and use onSay(...) function in it.

Originally I had the script like this:
Lua:
function onSay(cid, words, param)
    local createitemscript = require("createitembyname")
    createitemscript.onSay(cid, words, param)
end

Sorry if u need to introduce/help me into lua.
I saw call of "require" like this on stackoverflow.
the name of the module you are trying to require makes me think that it is a talkaction to create items using the name...
This already exists in TFS and almost every engine that has ever existed.

Just delete that file and use the /i command
For example:
/i demon armor
/i crystal coin

in case you don't have this command in your datapack, then copy the one that already exists in TFS:
create_item.lua
talkactions.xml

Note: The next times you ask for help, don't forget to mention which engine you use, so we can help you more accurately.
 
the name of the module you are trying to require makes me think that it is a talkaction to create items using the name...
This already exists in TFS and almost every engine that has ever existed.

Just delete that file and use the /i command
For example:
/i demon armor
/i crystal coin

in case you don't have this command in your datapack, then copy the one that already exists in TFS:
create_item.lua
talkactions.xml

Note: The next times you ask for help, don't forget to mention which engine you use, so we can help you more accurately.
Yeaah, I know it. I have this command and it already works but I want to make command for my purposes to create whole set like knight set by 1 command :) Thats why I created my own command and here inside my command I want to use existing command from createitembyname.lua
That is case to educate myself in lua programming.

Edit: Thanks for mention about telling the engine version.
Its 0.6.4 and I will mention about it in next posts. Good point
 
Last edited:
Yeaah, I know it. I have this command and it already works but I want to make command for my purposes to create whole set like knight set by 1 command :) Thats why I created my own command and here inside my command I want to use existing command from createitembyname.lua
That is case to educate myself in lua programming.
You can declare your functions in global.lua or in libs/ files..

example
function myCustomFunction(parameter)
print(parameter)
end

then, in your talk action you do:
function onSay(cid, words, param)
myCustomFunction("im using my new function")
end
 
You can declare your functions in global.lua or in libs/ files..

example
function myCustomFunction(parameter)
print(parameter)
end

then, in your talk action you do:
function onSay(cid, words, param)
myCustomFunction("im using my new function")
end
Okay thats cool but what if I want use existing functions onSay from createitembyname.lua?
Every functions from every script are
Lua:
function onSay(cid, words, param)

If I call this function it doesnt know which one to use
 
Yeaah, I know it. I have this command and it already works but I want to make command for my purposes to create whole set like knight set by 1 command :) Thats why I created my own command and here inside my command I want to use existing command from createitembyname.lua
That is case to educate myself in lua programming.

Edit: Thanks for mention about telling the engine version.
Its 0.6.4 and I will mention about it in next posts. Good point
I don't know what function you want to require, but you should never require, I don't recommend using that function if you don't know exactly what you want to do

Here is an example of TFS 0.3.6 code that will create the items and deliver them to the player who invokes the command:
custom_create_item.lua
Lua:
local set = {
    "demon helmet",
    "demon armor",
    "demon legs",
    "demon shield",
    "boots of haste"
}

function onSay(cid, words, param)
    for _, itemName in ipairs(set) do
        doPlayerAddItem(cid, getItemIdByName(itemName), 1)
    end
    return false
end

talkactions.xml
XML:
<talkaction log="yes" words="/customitems" access="4" event="script" value="custom_create_item.lua"/>
 
I don't know what function you want to require, but you should never require, I don't recommend using that function if you don't know exactly what you want to do

Here is an example of TFS 0.3.6 code that will create the items and deliver them to the player who invokes the command:
custom_create_item.lua
Lua:
local set = {
    "demon helmet",
    "demon armor",
    "demon legs",
    "demon shield",
    "boots of haste"
}

function onSay(cid, words, param)
    for _, itemName in ipairs(set) do
        doPlayerAddItem(cid, getItemIdByName(itemName), 1)
    end
    return false
end

talkactions.xml
XML:
<talkaction log="yes" words="/customitems" access="4" event="script" value="custom_create_item.lua"/>
Yea thas has a sense but what if I really want to use that function from createitembyname.lua to use that flow from this function? In function in this .lua file there is a flow checking if item exists and so on...
Lets say I want force to use that function onSay(cid, words, param) from createitembyname.lua in mine script?
 
Okay thats cool but what if I want use existing functions onSay from createitembyname.lua?
Every functions from every script are
Lua:
function onSay(cid, words, param)

If I call this function it doesnt know which one to use
you will have to declare a new function as i've said.
then, 'onSay' u call that function..
you can see how the tools items are made, example: shovel, pick, rope.. and then u see how the multitool (tool gear/sneaky stabber) works..
they have shared functions.. it's almost the same that u want
 
Yea thas has a sense but what if I really want to use that function from createitembyname.lua to use that flow from this function? In function in this .lua file there is a flow checking if item exists and so on...
Lets say I want force to use that function onSay(cid, words, param) from createitembyname.lua in mine script?
You can use the function that has been declared in another file by executing the code in that file and saving the function in a local or global variable so that you can use it later

Lua:
dofile("data/talkactions/scripts/createitembyname.lua")

local myCustomFunc = onSay

function onSay(cid, words, param)
    return myCustomFunc(cid, words, param)
end

I don't understand why you would want to do something like this, but you must have your reasons, let me know if you have any other questions or if I can help you further
 
Solution
You can use the function that has been declared in another file by executing the code in that file and saving the function in a local or global variable so that you can use it later

Lua:
dofile("data/talkactions/scripts/createitembyname.lua")

local myCustomFunc = onSay

function onSay(cid, words, param)
    return myCustomFunc(cid, words, param)
end

I don't understand why you would want to do something like this, but you must have your reasons, let me know if you have any other questions or if I can help you further
Okay thanks for help. I will try it today and let u know if it works :)
Post automatically merged:

you will have to declare a new function as i've said.
then, 'onSay' u call that function..
you can see how the tools items are made, example: shovel, pick, rope.. and then u see how the multitool (tool gear/sneaky stabber) works..
they have shared functions.. it's almost the same that u want
Hmm I understood it right now. Declare a function is good but I wanted to use already existing. Will try Sarah's solution first. Thanks anyway
Post automatically merged:

You can use the function that has been declared in another file by executing the code in that file and saving the function in a local or global variable so that you can use it later

Lua:
dofile("data/talkactions/scripts/createitembyname.lua")

local myCustomFunc = onSay

function onSay(cid, words, param)
    return myCustomFunc(cid, words, param)
end

I don't understand why you would want to do something like this, but you must have your reasons, let me know if you have any other questions or if I can help you further
It works, this is what I wanted. Thanks :)
 
Last edited:

Similar threads

Back
Top