This code isn't working


function GM:PlayerInitialSpawn(ply)

GAMEMODE:PlayerSpawnAsSpectator( ply )

end


This code, for some reason, doesn’t spawn the player as a spectator. I need this to work, though, because I want to do it the first time a player spawns… Am I using the wrong hook? Did I do something wrong? I need help…

[editline]Edit:[/editline]

I found out that PlayerInitialSpawn() is called when the player is still loading into the game, so what hook would I use that is called whenever a player has spawned but can be called only the first time the player spawns?

You could just use PlayerSpawn but add an “initialSpawn” bool on the player. So you’d only call the spectator code if “ply.initialSpawn == false” then set it to true.



function GM:PlayerSpawn(ply)
     if(!ply.initialSpawned)then
          GAMEMODE:PlayerSpawnAsSpectator(ply)
          ply.initialSpawned = true
     end
end


Thanks!

[editline]29th May 2015[/editline]

Wait, does this change the variable for the player or all players on the server at once?

If it affects all players when the bool is changed, I need to do this only to the player.

[editline]29th May 2015[/editline]

Lolcats why did you disagree? I tried the code and it worked…



function GM:PlayerInitialSpawn(ply)
timer.Simple(1,function()
GAMEMODE:PlayerSpawnAsSpectator(ply)
end)
end


This will set the boolean ONLY for the player spawning in at the time. Not all players at once. It’ll do exactly what you wish it to!

[editline]29th May 2015[/editline]

Not everyones computer will take less than 1 second to load after PlayerInitialSpawn is called. Do not use this approach.

Question, in a multiplayer game whenever I walk(after my first spawn) I fly upwards a bit, and I believe this might be because I still have ply:Spectate on? How would I disable it and make myself move like normal?

Never mind, after putting “else ply:UnSpectate()” in the if statement it works fine when I walk

ply:UnSpectate() or ply:Spawn()

[editline]30th May 2015[/editline]

Fair enough I have a fairly decent machine, but when friends with average-bad PC’s joined, they were fine. No errors. I dont think it’ll become an issue.

You’re probably right. My computers literal feces though so people like me may have issues. It’s better to be safe and universally functional.

Question, how would I make UnSpectate only get called on the player’s second spawn? I don’t know if it’s okay to call UnSpectate every single time the player has spawned, though if it’s okay and it won’t cause any problems(and there isn’t a better way) then simply let me know.

I rated you disagree because you can do what Pigsy said rather than assigning unnecessary variables. For example:



function GM:PlayerInitialSpawn(ply)
    timer.Simple(0, function()
        GAMEMODE:PlayerSpawnAsSpectator(ply)
    end)
end


(Just tested and works)

Creating a timer for any time will make the server wait for the player object to become valid. I don’t know how it works, but it does. Welcome to GLua.

As for their second spawn, you could just do something like



function GM:PlayerSpawn(ply)
    if ply:Deaths() >= 1 then
        ply:UnSpectate()
    end
end


The only problem with this is if you’re doing something round related, I don’t believe Deaths and Frags are reset between rounds. However since you’re making the gamemode, I suppose you can figure that out yourself.

Hmm. But, don’t forget that the variables help with using Player:UnSpectate.

It’s because the player gets fully initiated at the PlayerInitialSpawn gamemode function, but all the hooks get called before that. By adding a timer of any lenght(Might as well be 0) the function inside it gets moved to the end of the current callstack, which in the case of hooks will be after the gamemode function for that hook.

Or because PlayerSpawn (Which is called after PlayerInitialSpawn) calls ply:UnSpectate() by default.

Calls unspectate by default? Weird, whenever I spawned… or wait, since I overrided the hook that might be why it didn’t call it… I might just have to use hook.Add?