Question about PLY and variable scope

I am a bit confused about variable scope. Basically I tried to move every connected player to the spectator team.

[lua]
---------------------------------------------------------
Name: gamemode:PlayerConnect( )
Desc: Player has connects to the server (hasn’t spawned)
---------------------------------------------------------
/
function GM:PlayerConnect( name, address )

ply:KillSilent( )

ply:SetTeam( TEAM_SPEC )
ply:Spectate( OBS_MODE_ROAMING )

print (name,"…has connected, moving to spectators.")
end

[/lua]

Now as expected there is an error talking about accessing global variables. I have a vague idea that the problem is that this function does not know what the local player object is (which is ply?).

The player hasn’t spawned yet - the player entity does not exist. Use the PlayerInitialSpawn function instead.

Placed it in initialspawn:

[lua]
/---------------------------------------------------------
Name: gamemode:PlayerInitialSpawn( )
Desc: Called just before the player’s first spawn
---------------------------------------------------------
/
function GM:PlayerInitialSpawn( pl )

pl:SetTeam( TEAM_UNASSIGNED )

ply:KillSilent( )

ply:SetTeam( TEAM_SPEC )
ply:Spectate( OBS_MODE_ROAMING )

end

[/lua]

No errors, but nothing happens after pl:SetTeam( TEAM_UNASSIGNED ).

pl != ply

What is the difference? player,pl,ply looked like they are the same

No. They are variables that represent the player entity (And player is a library, not a variable. Using it as a variable will overwrite the library.)

You passed pl in the function, therefore ply is nil, pl is the player entity that spawned.

Setting all to PL still won’t run the code. So 1) What is wrong with my initial spawn function? and 2) What is the difference between pl and ply?

ply is nil, pl is not. Additionally, Lua is case sensitive - IE PL is nil too.

I think you mean I should not mix ply and pl in the same function because pl was passed and not ply. But is there any difference outside of this between pl and ply? Also I used this code and anything after UNASSIGNED won’t run.

[lua]
/---------------------------------------------------------
Name: gamemode:PlayerInitialSpawn( )
Desc: Called just before the player’s first spawn
---------------------------------------------------------
/
function GM:PlayerInitialSpawn( pl )

pl:SetTeam( TEAM_UNASSIGNED )

pl:KillSilent( )

pl:SetTeam( TEAM_SPEC )
pl:Spectate( OBS_MODE_ROAMING )

end

[/lua]

Did you define TEAM_UNASSIGNED?

EG:

[lua]TEAM_UNASSIGNED = 0
TEAM_A = 1
TEAM_B = 2[/lua]

I never define that team, however I defined 3 other teams, which I can switch to, if I use custom console commands or after the regular spawn function.
Also TEAM_UNASSIGNED does actually work, because every new player joining is on that team. So I do not think this is an issue with broken teams. It appears to me that Initialspawn seems to skip any functionality AFTER pl:SetTeam( TEAM_UNASSIGNED ),
Which are in this function:

pl:KillSilent( )

pl:SetTeam( TEAM_SPEC )

pl:Spectate( OBS_MODE_ROAMING )

My team definition:

[lua]

/* Teams */

TEAM_SNIPER = 1
TEAM_SOLDIER = 2
TEAM_SPEC = TEAM_SPECTATOR

team.SetUp(TEAM_SNIPER, “Snipers”, Color(150, 100, 100, 255), false)
team.SetUp(TEAM_SOLDIER, “Squad”, Color(150, 200, 150, 255), false)
team.SetUp(TEAM_SPEC, “Spectators”, Color(150, 150, 200, 255), true)

[/lua]

Although team UNASSIGNED is not defined here, it does exist, because I am on that team by default when joining. I guess on initialspawn the player is not really “completely” present in the gameworld yet, which causes it to skip the silentkill.

[editline]06:33PM[/editline]

Anyone got an idea how to kil the player on the first spawn?


TEAM_SPEC = TEAM_SPECTATOR

Why not just use TEAM_SPECTATOR?

Is UNASSIGNED defined in the base gamemode? I don’t remember, I haven’t touched lua in a while.

It has to be, because I am in this team when I join.
To repeat my question How to kill the player on the first spawn?

I found a reliable way:

[lua]
if (pl:Team( ) ~= ( TEAM_SNIPER )) and (pl:Team( ) ~= ( TEAM_SQUAD )) then

pl:KillSilent( )
pl:SetTeam( TEAM_SPEC )
pl:Spectate( OBS_MODE_ROAMING )
end

[/lua]

Ther is one issue left, this above if condition appears to work because there are no errors ingame, but the NOT TEAM_SQUAD condition is completely ignored. What is wrong here?

I’m sorry, I had to point this out again :smug:

Gbps are you addicted to big yellow ugly smileys looking like assholes?

Did not know this is the diss the newb section.

Inside the mind of Lua newbie section
[lua]
if(correctGrammar(threadStartingPost)) then
beMature()
else
flameWars()
end[/lua]

[lua]
if (pl:Team( ) ~= ( TEAM_SNIPER )) and (pl:Team( ) ~= ( TEAM_SQUAD )) then

pl:KillSilent( )
pl:SetTeam( TEAM_SPEC )
pl:Spectate( OBS_MODE_ROAMING )
end

[/lua]

What is wrong here? There are no lua errors, however the TEAM_SQUAD condition is ignored. This is part of the spawn function. When a player joins, and he is not on one of these teams, he should get killed and placed on spectator team.

Why don’t you post all your code rather than making us guess?