SpawnFunction Error

Hey Facepunch! I have a issue with my skeleton gamemode i currently made 4 different teams. With random weapons, but that doesn’t seems to be the issue. When i join the server, I want to become instant:: Team_1. Instead of a man there is StripWeapons. I’ve been searching for hour’s and i can’t find a salution. Please help me :). I’t might be a little confusing so i’ll say it again… When i join the server, the default spawn team should be:: Team_1 but it ain’t

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

include( ‘shared.lua’ )

// Serverside only stuff goes here

/---------------------------------------------------------
Name: gamemode:PlayerLoadout( )
Desc: Give the player the default spawning weapons/ammo
---------------------------------------------------------
/
local PlayerLoadout = PlayerLoad

local function GM:PlayerLoad( ply )–“When the player first joins the server and spawns” function

LocalPlayer:SetTeam(1)

end --End the “when player first joins server and spawns” function
hook.Add( “playerinitialspawn”, SpawnPly, GM:PlayerLoad() )

if ply:Team() == 1 then

ply:Give( "weapon_physcannon" ) --Give the player the Gravity Gun
ply:SetModel( "models/player/kleiner.mdl" ) --Set the players model
ply:Give( "item_battery", 15 ) --Give them 5 battries

elseif ply:Team() == 2 then

ply:Give( "weapon_physgun" )
ply:SetModel( "models/player/Eli.mdl" )
ply:Give( "toolgun" )

elseif ply:Team() == 3 then

ply:Give( "weapon_pistol" )
ply:GiveAmmo( 180, "pistol" ) --Give the player 180 pistol rounds
ply:SetModel( "models/player/barney.mdl" )

elseif ply:Team() == 4 then

ply:Give( "weapon_physgun" )
ply:Give( "weapon_357" )
ply:GiveAmmo( 32, "357" )
ply:SetModel( "models/player/gman_high.mdl" )
ply:Give( "item_battery", 15 ) --Give them 5 battries

end

function team_1( ply )

 ply:SetTeam( 1 ) //Make the player join team 1 
 ply:Spawn()

end

function team_2( ply )

 ply:SetTeam( 2 ) //Make the player join team 2 
 ply:Spawn()

end

function team_3( ply )

 ply:SetTeam( 3 ) //Make the player join team 3	 
 ply:Spawn()

end

function team_4( ply )

 ply:SetTeam( 4 ) //Make the player join team 4 
 ply:Spawn()

end
–And after the two previous console commands add
concommand.Add( “team_1”, team_1 ) //Add the command to set the players team to team 1
concommand.Add( “team_2”, team_2 ) //Add the command to set the players team to team 2
concommand.Add( “team_3”, team_3 ) //Add the command to set the players team to team 3
concommand.Add( “team_4”, team_4 ) //Add the command to set the players team to team 4[/lua]

Yaaay, 100 posts!!

[lua]local PlayerLoadout = PlayerLoad

local function GM:PlayerLoad( ply )–“When the player first joins the server and spawns” function

LocalPlayer:SetTeam(1)

end --End the “when player first joins server and spawns” function
hook.Add( “playerinitialspawn”, SpawnPly, GM:PlayerLoad() )[/lua]

There is so much wrong here.
First, there is no “PlayerLoad” hook, as your initial expression suggests, only “PlayerLoadout”.
Second, in order to use a hook you EITHER go directly to the gamemode table using function GM:Something() OR you use hook.Add, but not both. See example below.
Third, hooks are case sensitive so it’s “PlayerInitialSpawn”, not “playerinitialspawn”.
Fourth, unique names for hooks must be strings. Right now you’re calling some nil variable SpawnPly, so it should be in quotes – “SpawnPly”.
Fifth, this is the server, so you can’t call LocalPlayer. Rather, you want to use the player argument provided by the PlayerInitial Spawn Hook.
Sixth, your if ply:Team() == 1 statement needs to go inside a PlayerLoadout hook, whereas your ply:SetTeam(1) statement should go in your PlayerInitialSpawn hook.

So, you probably won’t read any of that so I’ll just give you your revised code anyway.

[lua]local function SetTeamOnSpawn(ply)
ply:SetTeam(1)
end
hook.Add( “PlayerInitialSpawn”, “SetTeamOnSpawn”, SetTeamOnSpawn )

local function GiveTeamWeapons(ply)
if ply:Team() == 1 then

	ply:Give( "weapon_physcannon" ) --Give the player the Gravity Gun
	ply:SetModel( "models/player/kleiner.mdl" ) --Set the players model
	ply:Give( "item_battery", 15 ) --Give them 5 battries
  
elseif ply:Team() == 2 then
  
	ply:Give( "weapon_physgun" )
	ply:SetModel( "models/player/Eli.mdl" )
	ply:Give( "toolgun" )
  
elseif ply:Team() == 3 then
  
	ply:Give( "weapon_pistol" )
	ply:GiveAmmo( 180, "pistol" ) --Give the player 180 pistol rounds
	ply:SetModel( "models/player/barney.mdl" )
  
elseif ply:Team() == 4 then
  
	ply:Give( "weapon_physgun" )
	ply:Give( "weapon_357" )
	ply:GiveAmmo( 32, "357" )
	ply:SetModel( "models/player/gman_high.mdl" )
	ply:Give( "item_battery", 15 ) --Give them 5 battries
  
end

end
hook.Add( “PlayerLoadout”, “GiveTeamWeapons”, GiveTeamWeapons )[/lua]

Thank you for your respond. But ehh… When you join you do not spawn as Team_1. And No matter what team you choice you have all guns, not the default weapons… Same when you aren’t choired Team :confused:

At the beginning of your PlayerLoadout hook run ply:StripWeapons().

You want me to Replace [lua]hook.Add( “PlayerLoadout”, “GiveTeamWeapons”, GiveTeamWeapons )[/lua]
To [lua]hook.Add( “hook run ply:StripWeapons()”, “GiveTeamWeapons”, GiveTeamWeapons )[/lua]

No no, at the beginning of the function.

[lua]
local function GiveTeamWeapons(ply)
ply:StripWeapons()

if ply:Team() == 1 then
   
    ply:Give( "weapon_physcannon" ) --Give the player the Gravity Gun
    ply:SetModel( "models/player/kleiner.mdl" ) --Set the players model
    ply:Give( "item_battery", 15 ) --Give them 5 battries
   
elseif ply:Team() == 2 then
   
    ply:Give( "weapon_physgun" )
    ply:SetModel( "models/player/Eli.mdl" )
    ply:Give( "toolgun" )
   
elseif ply:Team() == 3 then
   
    ply:Give( "weapon_pistol" )
    ply:GiveAmmo( 180, "pistol" ) --Give the player 180 pistol rounds
    ply:SetModel( "models/player/barney.mdl" )
   
elseif ply:Team() == 4 then
   
    ply:Give( "weapon_physgun" )
    ply:Give( "weapon_357" )
    ply:GiveAmmo( 32, "357" )
    ply:SetModel( "models/player/gman_high.mdl" )
    ply:Give( "item_battery", 15 ) --Give them 5 battries
   
end

end
hook.Add( “PlayerLoadout”, “GiveTeamWeapons”, GiveTeamWeapons )
[/lua]

okay thanks, i’ll try that you get a respond in 2 mins.

EDIT: Ehm, it dosent seems anything has changed… I’ll give you my Init.

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

include( ‘shared.lua’ )

// Serverside only stuff goes here

/---------------------------------------------------------
Name: gamemode:PlayerLoadout( )
Desc: Give the player the default spawning weapons/ammo
---------------------------------------------------------
/
local function SetTeamOnSpawn(ply)
ply:SetTeam(1)
end
hook.Add( “PlayerInitialSpawn”, “SetTeamOnSpawn”, SetTeamOnSpawn )

local function GiveTeamWeapons(ply)
ply:StripWeapons()

if ply:Team() == 1 then
    
    ply:Give( "weapon_physcannon" ) --Give the player the Gravity Gun
    ply:SetModel( "models/player/kleiner.mdl" ) --Set the players model
    ply:Give( "item_battery", 15 ) --Give them 5 battries
    
elseif ply:Team() == 2 then
    
    ply:Give( "weapon_physgun" )
    ply:SetModel( "models/player/Eli.mdl" )
    ply:Give( "toolgun" )
    
elseif ply:Team() == 3 then
    
    ply:Give( "weapon_pistol" )
    ply:GiveAmmo( 180, "pistol" ) --Give the player 180 pistol rounds
    ply:SetModel( "models/player/barney.mdl" )
    
elseif ply:Team() == 4 then
    
    ply:Give( "weapon_physgun" )
    ply:Give( "weapon_357" )
    ply:GiveAmmo( 32, "357" )
    ply:SetModel( "models/player/gman_high.mdl" )
    ply:Give( "item_battery", 15 ) --Give them 5 battries
    
end

end
hook.Add( “PlayerLoadout”, “GiveTeamWeapons”, GiveTeamWeapons )

function team_1( ply )

 ply:SetTeam( 1 ) //Make the player join team 1 
 ply:Spawn()

end

function team_2( ply )

 ply:SetTeam( 2 ) //Make the player join team 2 
 ply:Spawn()

end

function team_3( ply )

 ply:SetTeam( 3 ) //Make the player join team 3	 
 ply:Spawn()

end

function team_4( ply )

 ply:SetTeam( 4 ) //Make the player join team 4 
 ply:Spawn()

end
–And after the two previous console commands add
concommand.Add( “team_1”, team_1 ) //Add the command to set the players team to team 1
concommand.Add( “team_2”, team_2 ) //Add the command to set the players team to team 2
concommand.Add( “team_3”, team_3 ) //Add the command to set the players team to team 3
concommand.Add( “team_4”, team_4 ) //Add the command to set the players team to team 4
[/lua]

Try instead of hooking just.

function GM:PlayerLoadout(ply)

and

function GM:PlayerInitialSpawn(ply)

You’ll probably have more luck.

[editline]24th February 2011[/editline]

Try instead of hooking just.

function GM:PlayerLoadout(ply)

and

function GM:PlayerInitialSpawn(ply)

You’ll probably have more luck.

Nothing changed. Still same issue.

EDIT:
This is currently what i’v done.

[lua]function ( “GM:PlayerInitialSpawn(ply)”, “SetTeamOnSpawn”, “SetTeamOnSpawn” )

local function GiveTeamWeapons(ply)
ply:StripWeapons()[/lua]

Now i’m getting this Error

[lua][gamemodes\cakemod\gamemode\init.lua:16] ‘<name>’ expected near ‘(’
[cpp]
There was a problem opening the gamemode file ‘CakeMod/gamemode/init.lua’

[/lua]

You should really learn Lua’s syntax.

Instead of local function GiveTeamWeapons(ply)

function GM:PlayerLoadout(ply)

and instead of local function SetTeamOnSpawn(ply)

function GM:PlayerInitialSpawn(ply)

Current code.
[lua]local function GM:PlayerInitialSpawn(ply)
ply:SetTeam(1)
end
hook.Add( “PlayerInitialSpawn”, “SetTeamOnSpawn”, GM:PlayerInitialSpawn )[/lua]

[lua][gamemodes\cakemod\gamemode\init.lua:13] ‘(’ expected near ‘:’
[cpp]
There was a problem opening the gamemode file ‘CakeMod/gamemode/init.lua’

[/lua]

You don’t need to use hook.Add for GM:PlayerInitialSpawn.

Holy Christ it’s just me there is retarded, lol!

The only thing is, it ain’t forcing. But all the weapons and stuff seems to work correctly! :slight_smile:

[editline]25th February 2011[/editline]
Could it be possible it is something with the “shared”

What do you mean, not “forcing”?

When i join the server, i would like to be Team_1, but it’s not.

  • I’m just troling around with a Gravity gun! :smiley:

Paste all of your current code please.

In general, when you’re making an addon you use hook.Add(“PlayerSpawn”…)

If you’re making a gamemode you use GM:PlayerInitialSpawn(ply)

[editline]26th February 2011[/editline]

If you’re still using hook.Add for your PlayerInitialSpawn function, that might just be the problem. According to the wiki, when hooks are called it goes through all the hook.Add things first, and then calls the associated GM:… function.

So what might be happening is your hook is called, and the player gets team 1, but then it calls the PlayerInitialSpawn inherited from the base gamemode, and sets the team to unassigned.

At the moment i’m out of town, i’ll call my brother to give me the code…

MegaJohnny, thanks for it, i actually learned something. But i’m not sure how to fix it by my self :confused:

From he’s brother:

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

include( ‘shared.lua’ )

// Serverside only stuff goes here

/---------------------------------------------------------
Name: gamemode:PlayerLoadout( )
Desc: Give the player the default spawning weapons/ammo
---------------------------------------------------------
/
function GM:PlayerInitialSpawn(ply)
ply:SetTeam(1)
end
hook.Add( “PlayerInitialSpawn”, “SetTeamOnSpawn”, SetTeamOnSpawn )

function GM:PlayerLoadout(ply)
ply:StripWeapons()

if ply:Team() == 1 then
    
    ply:Give( "weapon_physcannon" ) --Give the player the Gravity Gun
    ply:SetModel( "models/player/kleiner.mdl" ) --Set the players model
    ply:Give( "item_battery", 15 ) --Give them 5 battries
    
elseif ply:Team() == 2 then
    
    ply:Give( "weapon_physgun" )
    ply:SetModel( "models/player/Eli.mdl" )
    ply:Give( "toolgun" )
    
elseif ply:Team() == 3 then
    
    ply:Give( "weapon_pistol" )
    ply:GiveAmmo( 180, "pistol" ) --Give the player 180 pistol rounds
    ply:SetModel( "models/player/barney.mdl" )
    
elseif ply:Team() == 4 then
    
    ply:Give( "weapon_physgun" )
    ply:Give( "weapon_357" )
    ply:GiveAmmo( 32, "357" )
    ply:SetModel( "models/player/gman_high.mdl" )
    ply:Give( "item_battery", 15 ) --Give them 5 battries
    
end

end
hook.Add( “PlayerLoadout”, “GiveTeamWeapons”, GiveTeamWeapons )

function team_1( ply )

 ply:SetTeam( 1 ) //Make the player join team 1 
 ply:Spawn()

end

function team_2( ply )

 ply:SetTeam( 2 ) //Make the player join team 2 
 ply:Spawn()

end

function team_3( ply )

 ply:SetTeam( 3 ) //Make the player join team 3  
 ply:Spawn()

end

function team_4( ply )

 ply:SetTeam( 4 ) //Make the player join team 4 
 ply:Spawn()

end
–And after the two previous console commands add
concommand.Add( “team_1”, team_1 ) //Add the command to set the players team to team 1
concommand.Add( “team_2”, team_2 ) //Add the command to set the players team to team 2
concommand.Add( “team_3”, team_3 ) //Add the command to set the players team to team 3
concommand.Add( “team_4”, team_4 ) //Add the command to set the players team to team 4
[/LUA]

I can’t see any problems with it now - the hook.Adds aren’t needed any more on lines 15 and 48, but that shouldn’t make any difference. Do you get any lua errors when loading the gamemode? If you do they might not show up on the screen so check the console as well.

Nope. I’m not getting any error’s, at all.

Deleted line 15, 48.