[Fretta] Recording players usernames for use within local function.

Hi all,

Basically, the Prop Hunt gamemode that I’m running on a server doesn’t indicate in chat when a player switches teams. As you can imagine, that is an issue, and so I’ve spent the past few hours digging in all of the source code to find where the actual team switch line was, and lo’ and behold, I found it within Fretta;

local func = function() RunConsoleCommand( "changeteam", ID ) end

The issue I face is that I’m not seeing anyway to turn this local function into something that can record the username of the switching player and display it in chat everytime the function is called. While getting the actual message to print is simple, I’m struggling to figure out how to record and insert the username of each player who switches within the gamemode into this chat message. The local function is apart of this larger function within the cl_selectscreen.lua file:

function GM:ShowTeam()

	if ( !IsValid( TeamPanel ) ) then 
		TeamPanel = vgui.CreateFromTable( vgui_Splash )
		TeamPanel:SetHeaderText( "Choose Team" )

		local AllTeams = team.GetAllTeams()
		for ID, TeamInfo in SortedPairs ( AllTeams ) do
			if ( ID != TEAM_CONNECTING && ID != TEAM_UNASSIGNED && ( ID != TEAM_SPECTATOR || GAMEMODE.AllowSpectating ) && team.Joinable(ID) ) then
				if ( ID == TEAM_SPECTATOR ) then
					TeamPanel:AddSpacer( 10 )
				local strName = TeamInfo.Name
				local func = function() RunConsoleCommand( "changeteam", ID )
				ChatPrint(  " have been swapped!")

				local btn = TeamPanel:AddSelectButton( strName, func )
				btn.m_colBackground = TeamInfo.Color
				btn.Think = function( self ) 
								self:SetText( Format( "%s (%i)", strName, team.NumPlayers( ID ) ))
								self:SetDisabled( GAMEMODE:TeamHasEnoughPlayers( ID ) ) 
				if (  IsValid( LocalPlayer() ) && LocalPlayer():Team() == ID ) then
					btn:SetDisabled( true )

Does anyone have any experience with this particular issue, or something similar? Any assistance would be incredible.

I believe

GM:OnPlayerChangedTeam will be the hook that helps you out

[editline]25th September 2016[/editline]

Just go ahead and hook.Add a function that announces the change. You’ll be just fine

Where should I place this? Inside the fretta code in OP or in the actual Prop Hunt code? Kinda new to GLua sorry :confused:

You can put it in as an “autorun/server/*.lua” and add it there. Or in the init.lua for prop hunt. Basically anywhere the server will load it.

Since you’re new make sure you’re using hook.Add and not defining the function again.

Alright, so I simply just place something like:

function GM:OnPlayerChangedTeam( Player ply, number oldTeam, number newTeam )
ChatPrint(  ply .. " has changed team!")

And then place this in the init file on the gamemode:

hook.Add (OnPlayerChangedTeam)

Everything should work?

Thank you for the help so far!

No no, I suggest you read up on the

hook.Add function. It’ll be your friend for adding things without overwriting any of the base.

With what you posted it would be:

hook.Add( "OnPlayerChangedTeam", "WHATEVER IDENTIFIER YOU WANT", function( ply, oldteam, newteam )


end )

[editline]25th September 2016[/editline]

ChatPrint is not going to work. I assume you want to useb

PrintMessage though be weary of the character limit.

In general the wiki will be very helpful for what works and what doesn’t. If you’re new, read up

Alright, so I simply drop something like this into the init file on the gamemode?

hook.Add( "OnPlayerChangedTeam", "ChangeNotif", function( ply, oldteam, newteam )

PrintMessage( HUD_PRINTTALK, ply .. " has changed team!" )

end )

That wont work.

Try putting this in a file in autorun/server/

hook.Add( "OnPlayerChangedTeam", "ChangeNotif", function( ply, oldteam, newteam )
PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has changed team!" )
end )

Please read the Wiki, instead of just putting something together and ask if it works.

hook.Add is explained in detail, and the examples show you how you should use the function. As for using PrintMessage, that would work.
It is also ill-advised to edit gamemode files, just put that inside lua/autorun/server/anyfile.lua

Apologies, I read the wiki and was just struggling to wrap my head around it as the examples seemed pretty barebones in context of making it work with an existing gamemode. Thank you to all who helped so far, I will test it right now!

[editline]26th September 2016[/editline]

Thanks to all the help here guys, I re read the wiki and I finally figured out hooks properly with the first example. Appreciate all the support, for anyone in future reading this thread, this is the code that worked;

function teamChange( ply, oldteam, newteam )
	PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has changed team!" )
hook.Add( "OnPlayerChangedTeam", "ChangeNotif", teamChange )

Didn’t really understand until now you’re adding the function above to that event occurring in game, and that the game handles the hook actually occurring. Knoxed code will also work, didn’t when I first ran it but the small changes I made to init in the actual gamemode may have been the root cause as to why not.

Regardless, thank you all!