I've this code,
hook.Add( "PlayerSwitchWeapon", function( ply, oldWeapon, newWeapon )
if (tostring(newWeapon) == tostring(hololink_swep) ) then
print( "This weapon is speciall" .. newWeapon:GetClass() .. "." );
end
end );
When I execute it, I get,
Error(s), warning(s): lua5.3: source_file.lua:1: attempt to index a
nil value (global 'hook') stack traceback: source_file.lua:1: in main
chunk [C]: in ?
You need to put your scripts in a specific folder to make them run inside the games engine. I suggest you put them into garrysmod/addons/YOURADDONNAME/lua/autorun/server or client, the realm depends on what you want to do.
Where you need to place what is explained pretty good in the wiki under this link.
Related
I keep getting this spammed in console:
MENU ERROR: lua/menu/mainmenu.lua:79: attempt to call global 'CanAddServerToFavorites' (a nil value)
lua/menu/mainmenu.lua:79 []
And this whenever I load a map (doesnt even let me play the map, kicks me out)
[ERROR] gamemodes/base/gamemode/animations.lua:333:
attempt to index local 'ply' (a nil value)
unknown - gamemodes/base/gamemode/animations.lua:333
I know the location of the files and I know that 333 or 79 means which line it is on, I just don't know how to fix the problem. Never coded in my life, but I tried changing the TranslateWeaponActivity to just TranslateActivity since I didnt see any other thing called TranslateWeaponActivity, that didn't work. Anyone with lua experience can help? Do I need to send the entire script?
function GM:TranslateActivity( ply, act )
local newact = ply:TranslateWeaponActivity( act )
-- select idle anims if the weapon didn't decide
if ( act == newact ) then
return IdleActivityTranslate[ act ]
end
return newact
end
and the code for favorites:
if ( self.CanAddServerToFavorites != CanAddServerToFavorites() ) then
self.CanAddServerToFavorites = CanAddServerToFavorites()
if ( self.CanAddServerToFavorites ) then
self.HTML:QueueJavascript( "SetShowFavButton( true )" )
else
self.HTML:QueueJavascript( "SetShowFavButton( false )" )
end
end
end
When I attempt to run this part of my code an error shows up and says that it expected a string but it got a function, I'm new to coding and I can't figure out what's wrong. here's the error,
lua: game:47: bad argument #1 to 'find' (string expected, got function)
stack traceback:
[C]: in function 'find'
game:47: in main chunk
[string "<eval>"]:11: in main chunk
and here's the code
if (string.find(location,patternno2) ~= nul) then
print(" You decide that you will live your life as a hermit, alone and isolated. GAME OVER")
return
end
local patternyes2 = '^[yY][eE]?[sS]?$'
if (string.find(location,patternyes2) ~= nul) then
print(" Great! You leave for the land of Yore.")
print("You arrive at the castle of the previously mentioned king.")
end
here's where location is assigned value
print(" Do you go to the quest location")
local location = io.read
Somebody can explain me this error ?? I'm new on Lua, and I wanna learn what I'm doing wrong!!
This is my Inventory.Lua
https://pastebin.com/KiUEajMm
I get this error:
ERROR: Unable to load module 'game_inventory': LUA ERROR:
/game_inventory/inventory.lua:117: attempt to index local 'itemWidget' (a nil value)
stack traceback:
[C]: in function '__index'
/game_inventory/inventory.lua:117: in function 'onInventoryChange'
/game_inventory/inventory.lua:77: in function 'refresh'
/game_inventory/inventory.lua:47: in function 'init'
/game_inventory/inventory.otmod:8:[#onLoad]:1: in main chunk
[C]: in function 'reloadModules'
/client_modulemanager/modulemanager.lua:149: in function 'reloadAllModules'
/client_modulemanager/modulemanager.otui:75: [#onClick]:2: in function </client_modulemanager/modulemanager.otui:75: [#onClick]:1>
The error means that you try to access a methode of itemWidget while it is nil. So if the possibility exits that itemWidget can be nil you have to check this:
if itemWidget == nil then
itemWidget:setStyle(InventorySlotStyles[slot])
...
end
Form the code you posted I can only guess why itemWidget is nil:
it is valid that inventoryPanel:getChildById('slot' .. slot) returns nil
a typo somewhere (e.g.: 'slot' .. slot instead of 'slot ' .. slot --> missing space)
wrong order: inventoryPanel:getChildById('slot' .. slot) only works after the init-function has ended or something similar
I am trying to create an Lua addon for Garry's Mod but I keep coming across an error in my code.
This is my code:
function say (Player, text, ent)
s = "/misc/custom/"..text
s2 = s..".mp3"
sound.PlayFile(s2)
end
hook.Add("PlayerSay", "Say", say)
And this is the resulting error.
[saysoundtest25] lua/autorun/chatsounds.lua:4: attempt to call field 'PlayFile' (a nil value)
1. v - lua/autorun/chatsounds.lua:4
2. unknown - lua/includes/modules/hook.lua:84
Any ideas?
User Robotboy655 on Facepunch helped me solve this! The final code:
hook.Add( "PlayerSay", "Say", function( ply, text, team )
BroadcastLua( 'surface.PlaySound("misc/custom/' .. text .. '.mp3")' )
end )
Thanks everyone for the help!
The /lua/autorun file is serverside and there is a variable sound server-side Lua, however only in client side does sound.PlayFile exist.
if SERVER then
AddCSLuaFile() -- We're the server running this code! Let's send it to the client
else -- We're a client!
-- Your code here, only ran by the client!
end
See the Garry's Mod wiki for more info and note the orange box on the page which means it's client side.
Please remember to check what the function takes also:
sound.PlayFile( string path, string flags, function callback )
Example (taken from the wiki)
sound.PlayFile( "sound/music/vlvx_song22.mp3", "", function( station )
if ( IsValid( station ) ) then station:Play() end
end )
Also more easy way of searching the docs:
http://glua.me/
http://glua.me/docs/#?q=sound.PlayFile
How DarkRP handles this:
https://github.com/FPtje/DarkRP/blob/master/gamemode/modules/chatsounds.lua#L275
I'm using a Lua Protected call, but I'm getting an unprotected error:
lua_getfield( m_L, LUA_GLOBALSINDEX, "startIteration" ); // 1
if( lua_isfunction( m_L, -1 ) ){
lua_pushnumber( m_L, n ); // 2
auto ret = lua_pcall( m_L, 1, 0, 0 ); // 0
checkLuaReturn( m_L, ret );
}else{
lua_pop( m_L, 1 ); // 0
}
And the error is:
PANIC: unprotected error in call to Lua API (../example/ex01.lua:31: attempt to index global 'raster' (a nil value))
And the Lua code is:
function startIteration( num )
io.write( "Start iteration " .. num .. "\n" )
raster.grass.save( "output.png" )
end
How can I solver it, to get a REAL protected call?
update: fixed extra pop
It seems like this has nothing to do with your lua_pcall() but rather with your lua code.
The line raster.grass.save( "output.png" ) is where the error is.
Try channging that to something like this:
if raster and type(raster) == 'table' then
if raster.grass and type(raster.grass) then
raster.grass.save()
end
end
But, that's not going to solve your issue, the issue is that you're calling a member of the variable raster before raster was even created.
I had the same problem, and it stumped me for a while. The answer on this thread misses the point of the question. OP is using lua_pcall, yet is still receiving an unprotected error.
In my case, the Lua stack was corrupted. I ran a function that did not return a value, but popped a value off the stack anyway. Later, when I ran lua_pcall on an unrelated function, I got an unprotected error. Correcting the error of popping off the stack fixed it.