Lua unprotected error using lua_pcall - lua

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.

Related

Garry's mod lua errors

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

attempt to index a nil value in lua

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.

gmod GameMode Lua. IsPlayer retuning nill value

I'm trying to make a gmod gamemode. In my init.lua I wanted it so that way team members can't hurt each other. So I used this code
function GM:EntityTakeDamage( target, dmginfo )
if ( target:IsPlayer() and dmginfo:IsPlayer() ) then
if (dmginfo:Team() == target:Team()) then
dmginfo:ScaleDamage( 0.0 ) // Sets damage to 0
end
end
end
However it's giving me the error telling me that IsPlayer() is a nil value even though it should be returning a boolean. It points to no other lines other then the line with IsPlayer() and it's saying it is IsPlayer()
you have a typo in line 3. dminfo
You should narrow down which of your multiple IsPlayer() calls actually is nil
dmgInfo is a CTakeDamageInfo which has no function IsPlayer()
single line Lua comments are opened with --, not //
https://wiki.garrysmod.com/page/Category:CTakeDamageInfo
If you call a function and it says its nil, then check if it even exists. Or even better, check this befor you use the function in the first place.
And to prevent you from coming back in a minute, CTtakeDamageInfo also does not have a function Team() as well.
Check out CTDamageInfo:GetAttacker()

Attempt to index "group" (A nill value)

Im new to Lua and can only just really read it, mainly just interpreting. I was having an error with a modification for Garrys mod where a lightsaber mod I had installed worked on client side but was invisible on server side. This is because of settings, ect. I asked on his group and he told me this :
You must call these functions on the weapon right after spawning it:
self:SetMaxLength( 42 )
self:SetCrystalColor( Vector( 255, 0, 0 ) )
self:SetDarkInner( false )
self:SetWorldModel( "models/... etc" )
self:SetBladeWidth( 2 )
self.LoopSound = "sound/lightsaber/..."
self.SwingSound = "sound/lightsaber/..."
self:SetOnSound( "sound/lightsaber/..." )
self:SetOffSound( "sound/lightsaber/..." )
self.WeaponSynched = true
Where self is the weapon.
So I put it into the code. All this does is completely remove the lightsaber and give me this error:
[ERROR] lua/weapons/weapon_lightsaber.lua:44: attempt to index global 'self' (a nil value)
1. unknown - lua/weapons/weapon_lightsaber.lua:44
Here is the pastebin of the code : http://pastebin.com/Y8kmivuv
Try using 'SWEP' instead of 'self'. self is usually defined in object-orientated code, which uses metatables in Lua.

Lua attempt to call field 'PlayFile' (a nil value)

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

Resources