attempt to index global 'GM' (a nil value) - Chat Box on Gamemode
12 replies, posted
Hi
I am working on a gamemode and recently received this error when running chat Commands Such As // for OOC:
[CODE][ERROR] gamemodes/santosrp/gamemode/sh_chat.lua:47: attempt to index global 'GM'
(a nil value)
1. unknown - gamemodes/santosrp/gamemode/sh_chat.lua:47[/CODE]
Below is the code for sh_chat. Before you ask, yes I did try using GAMEMODE instead of GM
[CODE]GM.Chat = {}
if SERVER then
function GM.Chat:PlayerSay( pPlayer, strText, bTeamOnly )
if strText:sub( 1, 5 ) == "/ads " then
if pPlayer:CanAfford( GAMEMODE.Config.AdvertPrice ) then
pPlayer:TakeMoney( GAMEMODE.Config.AdvertPrice )
pPlayer:AddNote( "Your advertisement cost you $".. string.Comma(GAMEMODE.Config.AdvertPrice) )
return strText
else
pPlayer:AddNote( "Advertisements cost $".. string.Comma(GAMEMODE.Config.AdvertPrice).. ". You can't afford that." )
return ""
end
elseif strText == "/unstuck" then
if GAMEMODE.Jail:IsPlayerInJail( pPlayer ) then
pPlayer:AddNote( "You may not use that command while in jail!" )
return ""
end
if pPlayer:InVehicle() then
pPlayer:AddNote( "You may not use that command while in a vehicle!" )
return ""
end
if pPlayer.m_intLastUnstuckTime then
if pPlayer.m_intLastUnstuckTime > CurTime() then
local time = math.Round( pPlayer.m_intLastUnstuckTime -CurTime() )
pPlayer:AddNote( "You must wait ".. time.. " seconds before trying this command again." )
return ""
end
end
GAMEMODE.Util:UnstuckPlayer( pPlayer )
pPlayer.m_intLastUnstuckTime = CurTime() +30
pPlayer:AddNote( "You should now be unstuck!" )
return ""
elseif strText:sub( 1, 11 ) == "/broadcast " then
if GAMEMODE.Jobs:GetPlayerJobID( pPlayer ) == JOB_MAYOR then return strText else return "" end
end
function GM.Chat:PlayerSay( pPlayer, strText, bTeamOnly )
if strText:sub( 1, 2 ) == "//" then
return ""
end
end
return
end
else
function GM.Chat:ModifyForChatTypes( tblArgs, pPlayer, strText )
local find = string.find( strText, "//" )
if find == 1 then
strText = string.sub( strText, find +2 )
strText = string.Trim( strText )
table.insert( tblArgs, Color(60, 200, 60) )
table.insert( tblArgs, "( OOC ) " )
return strText, -1
end
find = string.find( strText, "/ads" )
if find == 1 then
strText = string.sub( strText,find +5 )
table.insert( tblArgs, Color(60, 200, 60) )
table.insert( tblArgs, "( Advertisement ) " )
return strText, -1
end
--find = string.find(strText,"/a ")
--if find == 1 then
-- strText = string.sub(strText,find+3)
-- table.insert( tblArgs, Color(60, 200, 60) )
-- table.insert( tblArgs, "( Advertisement ) " )
-- return strText, -1
--end
find = string.find( strText, "/broadcast" )
if find == 1 then
strText = string.sub( strText,find +11 )
table.insert( tblArgs, Color(60, 200, 60) )
table.insert( tblArgs, "( Mayor Broadcast ) " )
return strText, -1
end
return strText, 500
end
function GM.Chat:OnPlayerChat( pPlayer, strText, bTeamOnly, bIsDead )
if not IsValid( LocalPlayer() ) then return end
local pos = LocalPlayer():GetPos()
local otherpos = IsValid( pPlayer ) and pPlayer:GetPos() or Vector( 0 )
local chatdist = 500
local tab = {}
strText, chatdist = self:ModifyForChatTypes( tab, pPlayer, strText )
if bTeamOnly then
if pPlayer == LocalPlayer() then
chat.AddText( Color(255, 100, 100), "Please speak in regular chat." )
end
return true
end
if chatdist > 0 then
if pos:Distance( otherpos ) > chatdist then
return true
end
end
if bIsDead then
table.insert( tab, Color(255, 30, 40) )
table.insert( tab, "*DEAD* " )
end
if IsValid( pPlayer ) then
table.insert( tab, pPlayer )
else
table.insert( tab, Color(0, 0, 0) )
table.insert( tab, "Console" )
end
table.insert( tab, Color(255, 255, 255) )
table.insert( tab, ": "..strText )
chat.AddText( unpack( tab ) )
return true
end
end[/CODE]
Thanks
Tom
[lua]if SERVER then
function GM.Chat:PlayerSay( pPlayer, strText, bTeamOnly )
-- ...
function GM.Chat:PlayerSay( pPlayer, strText, bTeamOnly )
if strText:sub( 1, 2 ) == "//" then
return ""
end
end
-- ...
end
else
-- ...
end[/lua]
You're defining a new GM.Chat:PlayerSay() inside the old one. You [i]probably[/i] didn't intend to do this.
[QUOTE=Luni;52404953][lua]if SERVER then
function GM.Chat:PlayerSay( pPlayer, strText, bTeamOnly )
-- ...
function GM.Chat:PlayerSay( pPlayer, strText, bTeamOnly )
if strText:sub( 1, 2 ) == "//" then
return ""
end
end
-- ...
end
else
-- ...
end[/lua]
You're defining a new GM.Chat:PlayerSay() inside the old one. You [i]probably[/i] didn't intend to do this.[/QUOTE]
Ahh Thank you did not notice that, however now doing // or /ads will throw this error in console:
[CODE]Error: hook->PlayerSay returned a non-string![/CODE]
Looks like a problem outside of your code. Maybe it's from the module system that's calling GM.Chat:PlayerSay?
A) When the script runs the first time its GM, after that it changes to GAMEMODE.
To be sure you can get both with gmod.GetGamemode() (The error in title say that GM doesn't exist)
B) Its strText:sub(strText, 1, 2 ) not strText:sub(1, 2 )
[QUOTE=Nak;52406393]
B) Its strText:sub(strText, 1, 2 ) not strText:sub(1, 2 )[/QUOTE]
Both [B]string.sub(strText, 1, 2)[/B] and [B]strText:sub(1, 2)[/B] are valid, but [B]strText:sub(strText, 1, 2)[/B] is not
Or even strText.sub(strText, 1, 2)
[QUOTE=Nak;52406393]A) When the script runs the first time its GM, after that it changes to GAMEMODE.
To be sure you can get both with gmod.GetGamemode() (The error in title say that GM doesn't exist)
B) Its strText:sub(strText, 1, 2 ) not strText:sub(1, 2 )[/QUOTE]
Already made it clear, tried Gamemode and besides, this is called on Startup of the server.
[QUOTE=code_gs;52405803]Looks like a problem outside of your code. Maybe it's from the module system that's calling GM.Chat:PlayerSay?[/QUOTE]
GM.Chat:PlayerSay is not called anywhere else in the Gamemode files, however in init.lua and cl_init.lua, this is called:
[CODE]function GM:PlayerSay( ... )
return self.Chat:PlayerSay( ... )
end[/CODE]
The last line of GM.Chat:PlayerSay is just [B]return[/B], change it to [B]return ""[/B] or [B]return strText[/B] (Whatever you need there), as long as you return a string
[QUOTE=JasonMan34;52406829]The last line of GM.Chat:PlayerSay is just [B]return[/B], change it to [B]return ""[/B] or [B]return strText[/B] (Whatever you need there), as long as you return a string[/QUOTE]
Well, A semi positive outcome :-)
// Now does not return anything, however /ads Still returns:
[CODE]Error: hook->PlayerSay returned a non-string!
Tom: /ads Test[/CODE]
Replace [B]return strText[/B] with [B]return self.BaseClass:PlayerSay(pPlayer, strText, bTeamOnly)[/B]
[QUOTE=JasonMan34;52406891]Replace [B]return strText[/B] with [B]return self.BaseClass:PlayerSay(pPlayer, strText, bTeamOnly)[/B][/QUOTE]
Removes the error thank you, however the Part's infront still do not show ( Advertisement ) and ( OOC )
Simple Server Reinstall and fresh GM install seems to work
Thanks All
Sorry, you need to Log In to post a reply to this thread.