Garry's mod lua errors - lua

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

Related

Roblox studio error "Attempt to concatenate string with Instance"

So I got this error and I cant seem to fix it.
Can anyone tell me how to fix it?
The error was : Attempt to concatenate string with Instance.
Image of the full error
The function:
function chatfunc(msg) -- < error line 523
coroutine.wrap(function()
local amountsofchats = 0
for i,v in pairs(workspace:GetChildren()) do
if v.Name == "amogus"..plr then
amountsofchats += 1
end
end
if amountsofchats >= 5 then
return
end
for i,v in pairs(workspace:GetChildren()) do
if v.Name == "amogus"..plr then
v.StudsOffset += Vector3.new(0,2,0)
end
end
...
The second error thing:
game:GetService("Players")[Username].Chatted:Connect(function(msg)
local msg,Message_ = msg,msg
if string.sub(msg,1,3) == "/e " then
msg = string.sub(msg,4)
end
chatfunc(msg) -- < error line 717
end)
You are getting this error because are you trying to concat a string with an instance for example "string" .. Instance.new("Part").
Now since you didnt give any lines numbers with your code, I am assuming "amogus"..plr is what causes the error, since plr is here an instance you probably ment to do "amogus"..plr.Name which is concatting two strings
Also
i do not recommend doing this at all game:GetService("Players")[Username] if the user has name which is a property of Players service u will get the property instead of the player. for example if you have the username MaxPlayers that will return a number and not the player with that name thus your code will error. So I recommend doing game:GetService("Players"):FindFirstChild(Username)

Attemp to call a nil value (field ‘ShowInventory’) [ESX2]

I just installed ESX 2 into my new server, im Really new at lua and i dont really know what i should do with some resources or how to start coding
I wan to work in the inventory system based on es_extended, but, it doesnt work.
I press the Inventory Key informed in the cofig file \server-data\resources\es_extended\config\default\config.lua
“Config.InventoryKey = “REPLAY_START_STOP_RECORDING_SECONDARY” – Key F2 by default”
module.InitESX = function()
module.RegisterControl(module.Groups.MOVE, module.Controls[Config.InventoryKey])
module.On('released', module.Groups.MOVE, module.Controls[Config.InventoryKey], function(lastPressed)
print("Comment before show The Inventory")
ESX.ShowInventory()
-- if Menu.IsOpen ~= nil then
-- if (not ESX.IsDead) and (not Menu.IsOpen('default', 'es_extended', 'inventory')) then
-- ESX.ShowInventory()
-- end
-- end
end)
end
I literally change a little bit the code to directly execute the ShowInventory() Function but i got this error
the original code look like this
module.InitESX = function()
module.RegisterControl(module.Groups.MOVE, module.Controls[Config.InventoryKey])
module.On('released', module.Groups.MOVE, module.Controls[Config.InventoryKey], function(lastPressed)
if Menu.IsOpen ~= nil then
if (not ESX.IsDead) and (not Menu.IsOpen('default', 'es_extended', 'inventory')) then
ESX.ShowInventory()
end
end
end)
end
but when i press the key dont do nothing and doesnt show anything in the console.

How to run a Function on a Player GMOD DarkRP Lua

I am trying to make a muting script that doesn't let players talk. At the moment I am at telling which player in the script with no commands but I cant work out or find how to run this function that I created on a specific player, not just mute the entire server.
sv_mute.lua:
util.AddNetworkString( "mute_message" )
ismuted == false
targetPlayer == "Xx_Player_xX"
function checkmute()
function SendMessage( ply, txt, pub )
net.Start( "mute_message" )
net.WriteString( "YOU ARE MUTED, SHUT UP" )
net.Send( ply )
return ""
end
hook.Add( "PlayerSay", "SendMessage", SendMessage, )
end
Player:checkmute(()targetPlayer)
cl_mute.lua:
function ReceiveMessage()
local txt = net.ReadString()
chat.AddText( Color( 0, 255, 0), txt)
end
I have so far got to using Player:checkmute(()targetPlayer) but I assume this is wrong
So at first a few mistakes I have seen on a first quick look.
You defined a function without a parameter but you try to call it with one
You did not used the tools Garry gave you already to send messages to players
Things we will be using to achieve your desired task
GM:PlayerSay( Player sender, string text, boolean teamChat )
Player:PrintMessage( number type, string message )
The solution
-- sv_mute.lua
-- a list of muted players, you need to get them from somewhere (not part of this answer)
mutedPlayers = {
"StupidMan" = true,
"AnnoyingKid" = true,
"ExGirlfriend" = true
}
-- the function that will check if a user is muted
local function checkMuted(--[[ string ]] playername)
return mutedPlayers[playername]
-- you probably need to change the code to something else
-- based on how you store your muted players
end
hook.Add("PlayerSay", "VosemMute_PlayerSay", function(sender, message, teamChat)
if ( checkMuted(sender:GetName()) ) then
sender.PrintMessage(HUD_PRINTTALK, "You are muted!")
return "" -- this will suppress the message
end
end)
Some helpful links
HUD Enumerations (HUD_PRINTTALK)
Player:GetName()
hook.Add( string eventName, any identifier, function func )
Garry's Mod Wiki
Lua Reference Manual (sadly i do not know which lua version is used by gmod)
Disclaimer
This code is completely untested and written of my mind. If this does not work and throws some errors let me know that. I'll correct it then.

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