• Executing commands for other players
    11 replies, posted
Hi, I want to create a "Stop Sound (Server)" button, but as far as I know, the 'stopsound' command is only clientside. [B]I'm here to ask you if it is possible to execute the commands for other players?[/B] [B]If yes[/B], I could execute the 'stopsound' command for every player on the server, but I have no idea how to do it. I hope I explained it well :D Sorry for English
[url]http://wiki.garrysmod.com/page/Global/RunConsoleCommand[/url]
No no no.. I know that. If I press the button, the same command will execute for all players on this server. Is this possible? Someone ever tried to do that?
Button? What kind of button? What I posted above will run a console command on all players.
Wait, what? I mean derma button. It's my button code: [CODE] local StopSoundButton = vgui.Create( "DButton" ) StopSoundButton:SetParent( DermaPanel ) StopSoundButton:SetText( "Stop Sounds (Client)" ) StopSoundButton:SetPos( 225, 560 ) StopSoundButton:SetSize( 125, 25 ) StopSoundButton.DoClick = function () RunConsoleCommand( "stopsound" ) end [/CODE] When I press this button (while music is on), it stops the music only for me, not for others (Because that code is in lua/autorun/client/cl_init.lua ? Should it be in lua/autorun/server/init.lua ?) //EDIT Wait, I need just do put "Global." before "RunConsoleCommand" ?
No, keep the file where it is, but replace the RunConsoleCommand with this. [code]for _, ply in pairs( player.GetAll() ) do ply:ConCommand( "stopsound" ) end[/code]
You forgot you'd need some networking or make a new concommand to allow the client to control other clients: [lua]concommand.Add( "stopsound_all", function( ply ) if not ply:IsAdmin() then return end -- NEVER TRUST THE CLIENT for k, ply in pairs( player.GetAll() ) do ply:ConCommand( "stopsound" ) end end ) if CLIENT then -- vgui stuff end[/lua] Concommand.Add needs to be run shared, so put this in a shared file and make sure the server does not load the vgui stuff by surrounding it with if CLIENT then ... end. Then change the RunConsoleCommand to RunConsoleCommand( "stopsound_add" ) to make the client run your custom console command instead of the default.
Actually it depends. Console commands can be in SERVER or CLIENT realms and the methods of execution are interesting. Here's a write-up to help understand what, when and where code executes in realms by using console commands. The only difference between this and standard code is that clients can't see server-code where-as clients can run server concommands. [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_tutorial_quizzes/explanation_of_realms.lua[/url]
[QUOTE=Acecool;45469292]Actually it depends. Console commands can be in SERVER or CLIENT realms and the methods of execution are interesting. Here's a write-up to help understand what, when and where code executes in realms by using console commands. The only difference between this and standard code is that clients can't see server-code where-as clients can run server concommands. [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_tutorial_quizzes/explanation_of_realms.lua[/url][/QUOTE] stopsound is clearly a clientside command and there's no way to change it.
[QUOTE=Robotboy655;45469440]stopsound is clearly a clientside command and there's no way to change it.[/QUOTE] My post referred to: "Concommand.Add needs to be run shared". Sorry for not being clear.......
ohh man.. @code_gs, your code didn't work :C . Friend still hears sounds when I press the button. I'm gonna test next code ;D @Internet1001, it should look like this? (or I should put all VGUI stuff here?) [CODE]concommand.Add( "stopsound_all", function( ply ) if not ply:IsAdmin() then return end for k, ply in pairs( player.GetAll() ) do ply:ConCommand( "stopsound" ) end end ) if CLIENT then local StopSoundButtonServer = vgui.Create( "DButton" ) StopSoundButtonServer:SetParent( DermaPanel ) StopSoundButtonServer:SetText( "Stop Sounds (Server)" ) StopSoundButtonServer:SetPos( 225, 560 ) StopSoundButtonServer:SetSize( 125, 25 ) StopSoundButtonServer.DoClick = function () for _, ply in pairs( player.GetAll() ) do ply:ConCommand( "stopsound" ) end end end [/CODE]
[QUOTE=Kubixster;45470565]ohh man.. @code_gs, your code didn't work :C . Friend still hears sounds when I press the button. I'm gonna test next code ;D @Internet1001, it should look like this? (or I should put all VGUI stuff here?) [CODE]concommand.Add( "stopsound_all", function( ply ) if not ply:IsAdmin() then return end for k, ply in pairs( player.GetAll() ) do ply:ConCommand( "stopsound" ) end end ) if CLIENT then local StopSoundButtonServer = vgui.Create( "DButton" ) StopSoundButtonServer:SetParent( DermaPanel ) StopSoundButtonServer:SetText( "Stop Sounds (Server)" ) StopSoundButtonServer:SetPos( 225, 560 ) StopSoundButtonServer:SetSize( 125, 25 ) StopSoundButtonServer.DoClick = function () for _, ply in pairs( player.GetAll() ) do ply:ConCommand( "stopsound" ) end end end [/CODE][/QUOTE] [code] concommand.Add( "stopsound_all", function( ply ) if not ply:IsAdmin() then return end for k, ply in pairs( player.GetAll() ) do ply:ConCommand( "stopsound" ) end end ) if CLIENT then local StopSoundButtonServer = vgui.Create( "DButton" ) StopSoundButtonServer:SetParent( DermaPanel ) StopSoundButtonServer:SetText( "Stop Sounds (Server)" ) StopSoundButtonServer:SetPos( 225, 560 ) StopSoundButtonServer:SetSize( 125, 25 ) StopSoundButtonServer.DoClick = function () LocalPlayer():ConCommand( "stopsound_all" ) end end [/code]
Sorry, you need to Log In to post a reply to this thread.