• This code isn't working
    13 replies, posted
[code]function GM:PlayerInitialSpawn(ply) GAMEMODE:PlayerSpawnAsSpectator( ply ) end [/code] 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. [CODE] function GM:PlayerSpawn(ply) if(!ply.initialSpawned)then GAMEMODE:PlayerSpawnAsSpectator(ply) ply.initialSpawned = true end end [/CODE]
Thanks! [editline]29th May 2015[/editline] [QUOTE=MrMicaww;47835134]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. [CODE] function GM:PlayerSpawn(ply) if(!ply.initialSpawned)then GAMEMODE:PlayerSpawnAsSpectator(ply) ply.initialSpawned = true end end [/CODE][/QUOTE] Wait, does this change the variable for [B][I]the[/I][/B] player or [B][I]all[/I][/B] players on the server at once? If it affects all players when the bool is changed, I need to do this only to [B][I]the[/I][/B] player. [editline]29th May 2015[/editline] Lolcats why did you disagree? I tried the code and it worked...
[QUOTE=A Fghtr Pilot;47835157]Thanks! [editline]29th May 2015[/editline] Wait, does this change the variable for [B][I]the[/I][/B] player or [B][I]all[/I][/B] players on the server at once? If it affects all players when the bool is changed, I need to do this only to [B][I]the[/I][/B] player. [editline]29th May 2015[/editline] Lolcats why did you disagree? I tried the code and it worked...[/QUOTE] [code] function GM:PlayerInitialSpawn(ply) timer.Simple(1,function() GAMEMODE:PlayerSpawnAsSpectator(ply) end) end [/code]
[QUOTE=A Fghtr Pilot;47835157]Thanks! [editline]29th May 2015[/editline] Wait, does this change the variable for [B][I]the[/I][/B] player or [B][I]all[/I][/B] players on the server at once? If it affects all players when the bool is changed, I need to do this only to [B][I]the[/I][/B] player. [editline]29th May 2015[/editline] Lolcats why did you disagree? I tried the code and it worked...[/QUOTE] 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] [QUOTE=Pigsy;47835252][code] function GM:PlayerInitialSpawn(ply) timer.Simple(1,function() GAMEMODE:PlayerSpawnAsSpectator(ply) end) end [/code][/QUOTE] Not everyones computer will take less than 1 second to load after PlayerInitialSpawn is called. Do not use this approach.
[QUOTE=MrMicaww;47835280]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.[/QUOTE] 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
[QUOTE=A Fghtr Pilot;47835328]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?[/QUOTE] ply:UnSpectate() or ply:Spawn() [editline]30th May 2015[/editline] [QUOTE=MrMicaww;47835280]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.[/QUOTE] 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.
[QUOTE=Pigsy;47835332]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.[/QUOTE] 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.
[QUOTE=Pigsy;47835332]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.[/QUOTE] 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: [code] function GM:PlayerInitialSpawn(ply) timer.Simple(0, function() GAMEMODE:PlayerSpawnAsSpectator(ply) end) end [/code] (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 [code] function GM:PlayerSpawn(ply) if ply:Deaths() >= 1 then ply:UnSpectate() end end [/code] 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.
[QUOTE=Lolcats;47835664]I rated you disagree because you can do what Pigsy said rather than assigning unnecessary variables. For example: [code] function GM:PlayerInitialSpawn(ply) timer.Simple(0, function() GAMEMODE:PlayerSpawnAsSpectator(ply) end) end [/code] (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.[/QUOTE] Hmm. But, don't forget that the variables help with using Player:UnSpectate.
[QUOTE=Lolcats;47835664]-snip-[/QUOTE] 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.
[QUOTE=Willox;47838961]Or because PlayerSpawn (Which is called after PlayerInitialSpawn) calls ply:UnSpectate() by default.[/QUOTE] 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?
Sorry, you need to Log In to post a reply to this thread.