• Start a console command on a player that is going to exit from the server
    20 replies, posted
Hello. Excuse me for any grammatical errors. I made a script that launch the console command "+attack" on a player and the console command "-attack" after 10 seconds. The problem is that if the player leaves the server when the command "-attack" hasn't been launched yet, when the player joins into another server, he'll continues to shoot until he will lauch the command "-attack". The unique solution I found is to use the hook "PlayerDisconnected" to start the console command "-attack" on the player's console, but I think that I can't do anything like this. Example of the code: hook.Add("PlayerDisconnected", "FixSomeBugs", function( ply ) ply:ConCommand("-attack") end) Thank you! [B]Fixed by [U]NeatNit[/U][/B] [B]First solution:[/B] hook.Add("StartCommand", "Force attack because I said so", function(ply, cmd) if PlayerShouldBeAttacking(ply) then cmd:SetButtons(bit.bor(cmd:GetButtons(), IN_ATTACK)) end end) Replace "PlayerShouldBeAttacking" with your own logic. [B]Second solution:[/B] [B]DON'T USE THIS, USE THE FIRST INSTEAD. But if there was some other reason you need to run a console command on the player when they leave a server, this is a way that works.[/B] hook.Add("ShutDown", "a", function() RunConsoleCommand("-attack") end)
I'm not sure if that will work since the player entity might be a replica by that point, but the best solution is to just use an alternative to your +attack call. What are you trying to do?
[QUOTE=Predda;52227840][...][I]The unique solution I found[/I] [...][/QUOTE] Calm down Steve Jobs, anyone can open the wiki and type "Disconnect" in the search bar. As for your issue, why are you forcing +attack in the first place? If you absolutely have to do it, try using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/gameevent/Listen]gameevent.Listen[/url] with [B]player_disconnect[/B].
[QUOTE=JasonMan34;52227873]Calm down Steve Jobs, anyone can open the wiki and type "Disconnect" in the search bar. As for your issue, why are you forcing +attack in the first place? If you absolutely have to do it, try using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/gameevent/Listen]gameevent.Listen[/url] with [B]player_disconnect[/B].[/QUOTE] All right, just take it easy... [editline]14th May 2017[/editline] [QUOTE=code_gs;52227871]I'm not sure if that will work since the player entity might be a replica by that point, but the best solution is to just use an alternative to your +attack call. What are you trying to do?[/QUOTE] I just would like to use the player's console commands, but I can't if I don't find a solution for this problem. (the script I made is only an example, I know that to shoot I could find another way, I just want to know if is possible use the console commands to do so)
But +attack isn't just any console command. It is seldom necessary to force a toggle (+/-) console command on the client, or anything that will remain once the player has disconnected for that matter. If we knew what you're trying to do we could help find a different solution
[QUOTE=JasonMan34;52227937]But +attack isn't just any console command. It is seldom necessary to force a toggle (+/-) console command on the client, or anything that will remain once the player has disconnected for that matter. If we knew what you're trying to do we could help find a different solution[/QUOTE] There isn't one thing that I want to do, but for example, if I would make the player jump, how can I do? I have an idea, but I haven't find anything on the wiki yet, if I could handle the keys of the client, I could, for example, force to press the key "IN_JUMP".
If you just need to force them to attack you should be doing this is in [url]http://wiki.garrysmod.com/page/GM/StartCommand[/url]
[QUOTE=TehBigA;52228013]If you just need to force them to attack you should be doing this is in [url]http://wiki.garrysmod.com/page/GM/StartCommand[/url][/QUOTE] Thank you, but I don't understand how should I use this hook
[QUOTE=Predda;52228024]Thank you, but I don't understand how should I use this hook[/QUOTE] [lua]cmd:SetButtons(bit.bor(cmd:GetButtons(), IN_ATTACK))[/lua] [editline]14th May 2017[/editline] For the sake of the People Of The Future coming across this thread for a different reason than +attack, and as a proof of concept, I just tried this and it works: [lua]hook.Add("ShutDown", "a", function() RunConsoleCommand("-attack") end)[/lua] [b]DON'T USE THIS, SWITCH TO MANIPULATING CUserCmd INSTEAD.[/b] But if there was some other reason you need to run a console command on the player when they leave a server, this is a way that works.
[QUOTE=NeatNit;52228047][lua]cmd:SetButtons(bit.bor(cmd:GetButtons(), IN_ATTACK))[/lua][/QUOTE] Excuse me another time but I'm inexperienced on the LUA, I don't know what is cmd, I looked in the wiki but there are no examples on this.
[QUOTE=Predda;52228079]Excuse me another time but I'm inexperienced on the LUA, I don't know what is cmd, I looked in the wiki but there are no examples on this.[/QUOTE] Sure thing. Here is a more complete example: [lua]-- this should be in a shared file hook.Add("StartCommand", "Force attack because I said so", function(ply, cmd) if PlayerShouldBeAttacking(ply) then cmd:SetButtons(bit.bor(cmd:GetButtons(), IN_ATTACK)) end end)[/lua] Replace "PlayerShouldBeAttacking" with your own logic. [editline]14th May 2017[/editline] Oh god, what part of "DON'T USE THIS" didn't you understand :v: use the example in this post
[QUOTE=NeatNit;52228093] Oh god, what part of "DON'T USE THIS" didn't you understand :v: use the example in this post[/QUOTE] I'm not using this :/
But you put it in the OP...? Whatever :P I hope you'll get it working now! As a rule of thumb, if you find yourself running console commands from Lua, there's [i]probably[/i] a better way to do it without console commands. This is especially true for user input.
[QUOTE=NeatNit;52228093]Oh god, what part of "DON'T USE THIS" didn't you understand :v: use the example in this post[/QUOTE] Some people don't really want to learn how to code, but rather make one thing (albeit, it usually ends up being a bunch of messy/horribly unoptimized "one things") I know I was like this for the longest time :/ I still have old codes I made where the only hook I used is Think :suicide:
[QUOTE=NeatNit;52228116]But you put it in the OP...? Whatever :P I hope you'll get it working now! As a rule of thumb, if you find yourself running console commands from Lua, there's [i]probably[/i] a better way to do it without console commands. This is especially true for user input.[/QUOTE] Thank you very much! The problem is that this method works only with the key IN_ATTACK, if I want for example "click" another key, like "IN_FORWARD", I can't with this method. Am I wrong? [editline]14th May 2017[/editline] [QUOTE=JasonMan34;52228130]Some people don't really want to learn how to code, but rather make one thing (albeit, it usually ends up being a bunch of messy/horribly unoptimized "one things")[/QUOTE] Thank you! Just so you know, he gave me a solution, but I'm still here to understand how make it in another way... Please stop judging and start helping
[QUOTE=Predda;52228134] Thank you! Just so you know, he gave me a solution, but I'm still here to understand how make it in another way... Please stop judging and start helping[/QUOTE] The only thing I "judged you for" was a particularly conceited choice of words, sorry 'bout that. And in my previous reply, I was justifying your decision to ignore his advice more than anything. As for the solution: Are you talking about the one you edited into your OP? The one he specifically said to never use? Because, as he said, that is a horrible solution that you should never use. It's better to use the StartCommand hook, with the example he gave: [QUOTE=NeatNit;52228093] [lua]-- this should be in a shared file hook.Add("StartCommand", "Force attack because I said so", function(ply, cmd) if PlayerShouldBeAttacking(ply) then cmd:SetButtons(bit.bor(cmd:GetButtons(), IN_ATTACK)) end end)[/lua][/QUOTE]
[QUOTE=JasonMan34;52228152] As for the solution: Are you talking about the one you edited into your OP? The one he specifically said to never use? Because, as he said, that is a horrible solution that you should never use.[/QUOTE] Excuse me, I did not understand what OP is. (I edited it, but I haven't totally removed the "bad" solution, because, as he said "if there was some other reason you need to run a console command on the player when they leave a server, this is a way that works")
[QUOTE=Predda;52228134]Thank you very much! The problem is that this method works only with the key IN_ATTACK, if I want for example "click" another key, like "IN_FORWARD", I can't with this method. Am I wrong?[/QUOTE] You can set any keys you want, see this page: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/CUserCmd/SetButtons]CUserCmd:SetButtons[/url] However, IN_FORWARD wouldn't work. From the wiki page: [quote]If you are looking to affect player movement, you may need to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/CUserCmd/SetForwardMove]CUserCmd:SetForwardMove[/url] instead of setting the keys.[/quote]
[QUOTE=NeatNit;52228266]However, IN_FORWARD wouldn't work. From the wiki page:[/QUOTE] If you're gonna mess with player movement, you might as well use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/SetupMove]GM:SetupMove[/url]
[QUOTE=JasonMan34;52228277]If you're gonna mess with player movement, you might as well use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/SetupMove]GM:SetupMove[/url][/QUOTE] No. There's player [i]movement[/i] and player [i]input[/i]. Two different things. Predda wants to basically force player input.
:snip:
Sorry, you need to Log In to post a reply to this thread.