Quick Help: '(' expected near 'GM'

So I’m trying to learn GLua and I’ve tried to make a simple script, however upon loading it, the script doesn’t work and gives the error



[ERROR] lua/autorun/test.lua:14: '(' expected near 'GM'
  1. unknown - lua/autorun/test.lua:0


My lua code



funnyColor = Color(255, 0, 0, 255)

function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
  if ( IsValid( player )) then
    player:SetColor( funnyColor )
    print("Yes")
  else
    player:SetColor( funnyColor )
    print("No!")
  return true
  end
end

hook.Add("OnPlayerChat","PlayerChatHook", function GM.OnPlayerChat() end )


I have no idea what I’m doing wrong. Thanks in advance for the help

This isn’t how you add hooks.



funnyColor = Color(255, 0, 0, 255)

function UniqueNameForYourHook( player, strText, bTeamOnly, bPlayerIsDead )
  if ( IsValid( player )) then
    player:SetColor( funnyColor )
    print("Yes")
  else
    player:SetColor( funnyColor )
    print("No!")
  return true
  end
end

hook.Add("OnPlayerChat","PlayerChatHook", UniqueNameForYourHook )


In your previous code, you were creating a function in the GM table, which will effectively be called as a hook, but this is terrible practice as it overwrites the core gamemode hook, and will lead to problems and even conflicts between addons.

Instead, you name it as a unique function, and pass that to the hook.Add function as its 3rd argument.

On a side note, the variable funnyColor need not be global, prefix it with "local ". Also, you should not return a value within a hook as this will stop all other hooks from being called (this will also cause conflicts with other addons).

Here is a more concise version of your code:



local funnyColor = Color(255, 0, 0, 255)

function UniqueNameForYourHook( player )

  if ( isValid(player ) ) then
    print( "Yes" )
    player:SetColor( funnyColor )
    return nil
  end

  print( "No!" )
  -- Do not color an invalid player
end

hook.Add("OnPlayerChat","PlayerChatHook", UniqueNameForYourHook )




function() GM.OnPlayerChat() end


You don’t even need to add the hook through hook.Add considering your overriding the gamemode hook.

You don’t want to override GM functions unless you’re creating your own gamemode. You should use hooks instead:
[lua]local function LocalizedFunction()
– do stuff
end

hook.Add(“EventName”, “Unique name for this hook”, LocalizedFunction) – referenced the funtion declared above[/lua]or[lua]hook.Add(“EventName”, “Unique name”, function()
– do stuff
end) – used anonymous function[/lua]
In your example, you’re doing both (overriding the GM function and trying to add it as a hook, which you shouldn’t do).

Also there’s no reason not to localize the funnyColor variable.

[editline]8th November 2017[/editline]

There’s no reason to ever do anything like this, and moreover, in this case it wouldn’t even work.

Doesn’t matter - the syntax error is fixed, which actually answers the first question. As for the logic behind actually doing it that way in the first place - I chose not to answer that.