button:DoClick() | Change team on press

Hey there again, guys!

I have another, well not problem but kinda question.

I have set up a Derma for a gamemode Im currently developing, everythings working fine. Theres a nice layout I designed and overall it looks amazing. Just one problem. I want the player to change to the teams I created, if they press a certain button.

This is a code for one button.


 local button = vgui.Create( 'DButton' )
			button:SetParent( base )
			button:SetPos( ScrW()/25 + 160, ScrH()/25 + 80 )
			button:SetSize( 100, 20 )
			button:SetText( 'Anarchist' )
			function button:Paint( w, h )
				draw.RoundedBox( 0, 0, 0, w, h, Color(222, 222, 222, 120 ) )
			end
			
			local In = false
			
			button.OnCursorEntered = function()
				In = true
				--surface.SetDrawColor(200, 220, 220, 255)
				--surface.DrawRect(0, 0, button:GetWide(), button:GetTall())
			end
			
			button.OnCursorExited = function()
				In = false
			end
			
			function button:Paint( w, h )
				if In then
				surface.SetDrawColor(222, 222, 222, 60 )
				surface.DrawRect(0, 0, button:GetWide(), button:GetTall())
				end
			end
			
			function button:DoClick()
				team.SetClass(2, teams )
			end 

Now the


function button:DoClick()
				team.SetClass(2, teams )
			end 

doesnt do anything, which is pretty sad.

This code is set up in the cl_init.lua.

I have those files included:


include("shared.lua")
include("database/cl_database.lua")
include("database/items.lua")

Now, how do I get the team of the player to change when he/she pressed this button.

In my player.lua file I have this set up:


local ply = FindMetaTable( "Player" )

local teams = {}

teams[1] = {name = "Enforcer", color = Vector( 0.0, 0.0, 0.0 ), weapons = {"weapon_crowbar", "weapon_pistol", "weapon_physcannon", "weapon_physgun","gmod_camera", "gmod_tool"} }
teams[2] = {name = "Anarchist", color = Vector( 0.0, 0.0, 0.0 ), weapons = {"weapon_crowbar", "weapon_pistol", "weapon_physcannon", "weapon_physgun","gmod_camera", "gmod_tool"} }

function ply:SetGamemodeTeam( n )
	if not teams[n] then return end
	
	self:SetTeam( n )
	
	self:SetPlayerColor( teams[n].color )
	
	self:GiveGamemodeWeapons()
	
	return true
end

function ply:GiveGamemodeWeapons()
	local n = self:Team()
	self:StripWeapons()
	
	for k, wep in pairs(teams[n].weapons) do
		self:Give(wep)
	end
end

I think I might have to include more files in certain scripts? Im not sure and I still didnt understand everything of lua. (As you may have already noticed, lol)

I would really much appreciate if you guys could help me with this. And maybe even explain a little bit to me, what I have done wrong.

Thank you so much! Im learning lua more and more every day, because people like YOU help me with this. <3!

You need to network it:

Example
clientside



function button:DoClick()
	net.Start("ChangeTeam")
              net.WriteFloat(2)
        net.SendToServer()
end


serverside



util.AddNetworkString("ChangeTeam")
net.Receive("ChangeTeam", function( len, ply )
      ply:SetGamemodeTeam( net.ReadFloat() )
end)


not tested, should work

Okay, thanks! Ill try to do that. God Im stupid, totally forgot about the network library -.-

[editline]18th May 2014[/editline]

It’s working perfectly fine. Thank you very much!