ply:SetTeam doesn't work

Thanks, it’s ok now, but when i write go_combine in console i get this error



] go_combine 
HLTF\gamemode\init.lua:39: attempt to call method 'SetTeam' (a nil value)


Why is SetTeam not existing?

cl_init.lua:

[lua]include( “init.lua” )

concommand.Add(“go_combine”, sb_team1)
concommand.Add(“go_rebel”, sb_team2)[/lua]

init.lua contains

[lua]AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include( “shared.lua” )

function GM:PlayerSpawn( ply )

self.BaseClass:PlayerSpawn( ply )   

ply:SetGravity( 0.75 )  
ply:SetMaxHealth( 100, true )  

ply:SetWalkSpeed( 325 )  
ply:SetRunSpeed( 100 ) 

end

function GM:PlayerInitialSpawn( ply )
CheckSpecialCharacters( ply )
if ply:IsAdmin() then
sb_team2( ply )
else
joining( ply )
RunConsoleCommand( “sb_start” )
end
end

function GM:PlayerLoadout( ply )
if ply:Team() == 1 then
ply:Give( “weapon_pistol” )

elseif ply:Team() == 2 then
	ply:Give( "weapon_pistol" )
	
end

end

function sb_team1( ply )
ply:SetTeam( 1 )
ply:Spawn()
ply:PrintMessage( HUD_PRINTTALK, "You are a Combine, "…ply:Nick() )
end

function sb_team2( ply )
ply:SetTeam( 2 )
ply:Spawn()
ply:PrintMessage( HUD_PRINTTALK, "You are a rebel, "…ply:Nick() )
end

[/lua]

shared.lua contains this:

[lua]GM.Name = “Half Life 2: Team Fortress”
GM.Author = “Tera”
GM.Email = “gabrycir@hotmail.it”

DeriveGamemode( “base” )

team.SetUp( 1, “Combine”, Color( 125, 125, 125, 255 ) )
team.SetUp( 2, “Resistance”, Color( 255, 255, 255, 255 ) )[/lua]

Move the concommands to init.lua and make cl_init.lua include shared.lua not init.lua

First, don’t include init.lua in cl_init.lua.

Also, you add concommands serverside, so this would work:

[lua]function sb_team1( ply, cmd, args )
ply:SetTeam( 1 )
ply:Spawn()
ply:PrintMessage( HUD_PRINTTALK, "You are a Combine, "…ply:Nick() )
end
concommand.Add(“go_combine”, sb_team1)[/lua]

EDIT: Wow lexic. 2 seconds before me D:

EDIT2: Actually it was like 30 minutes, but I opened this tab a long time ago and answered it with no answers posted yet :smiley:

Actually his reply was 28 minutes before yours. :open_mouth:

Instead of having a console command to set each team why not just make one console command and you pass the team through as a parameter? Less redundant code.

Off Topic - Are you from Darkland?

On Topic - As Emz just said - Lots of console commands make your code look un-neat.

Also, when I change team with go_combine or go_rebel, my ammo doesn’t get refilled, like if I spawn without dying. So I tried adding ply:Kill() before ply:Spawn() but I see no ragdoll, and the ammo is still not full. Why? How do I actually kill the player?

[editline]05:12PM[/editline]

Just read your answers, how do I do the single command thing?

Instead of ply:Spawn() use ply:KillSilent()

ply:KillSilent() basically kills you, but without alerting everyone with the death messages.

I use to be super admin there until I resigned, yes.

edit with a quick example:

[lua]function SetTeam( Player, Command, Arguments )
if( #Arguments < 1 ) then
return;
end

Player:SetTeam( tonumber( Arguments[1] ) );
Player:KillSilent();
Player:Spawn(); 

end
concommand.Add( “SetTeam”, SetTeam );[/lua]

Then to call it you’d do:
RunConsoleCommand( “SetTeam”, TeamNumber );

Thank you for the example, ill keep that in the cookbook :stuck_out_tongue:

Anyway now i wanna remove that console command thing and make a vgui instead. I already know how to make vgui frames and stuff, so the things i need are
-How to start a function when I press a key (to then display the frame)
-How to block the player from doing anything else and showing the mouse cursor while the vgui frame is up

Thank you guys for the help you are giving me

GUI’s use console commands too, so don’t remove that. Because GUIs are client side, they can’t run anything on the server, so you still need the concommands. To make a button run a concommand, just do this:

[lua]local team1btn = vgui.Create(“DButton”)
–Do all the other stuff like set parent, position, size, etc.
team1btn.DoClick = function ()
RunConsoleCommand(“sb_team1”) --Console command for player to run on button click
end[/lua]

And then you would make another button for the other team.

Then to bring up the menu on a button press, read this, which will show you how to detect a key input:

http://www.facepunch.com/showpost.php?p=16181233&postcount=10

And also the thread that came from.