• Net Broadcasting
    8 replies, posted
Hello, I am trying to create a bank robbery system for DarkRP. So far so good. Except I want it so when the bank is robbed. In this system you press a button in a derma then a networkstring is broadcast. Except one problem. Derma and dbuttons are on the client but net.Broadcast is called on the server... The code I am using is CL_INIT.lua: [CODE]include( "shared.lua" ) function ENT:Draw() self:DrawModel() end local BankBalance = 25000 timer.Create( "BankBalanceChanging", 180, 0, function() BankBalance = BankBalance + math.random( 2500, 15500 ) end) net.Receive( "OpenBankMenu", function() local Background = vgui.Create( "DFrame" ) Background:SetSize( 500, 500 ) Background:Center() Background:SetTitle("") Background:SetVisible( true ) Background:ShowCloseButton( true ) Background:SetDraggable( false ) Background.Paint = function(s, w, h) draw.RoundedBox( 5, 0, 0, w, h, Color(20,255,20,255)) end Background:MakePopup() local BalanceBut = vgui.Create( "DButton", Background ) BalanceBut:SetSize( 150, 30) BalanceBut:SetPos( 250, 250 ) BalanceBut:SetText("Bank Balance") BalanceBut:SetColor(Color(0,0,0,255)) function BalanceBut.DoClick() DarkRP.notify( 1, 10, "The Bank Balance is: $"..BankBalance ) Background:Close() end local CloseBut = vgui.Create( "DButton", Background ) CloseBut:SetSize( 150, 30 ) CloseBut:SetPos( 250+100, 250+100) CloseBut:SetText("Close") CloseBut:SetColor( Color(0,191,255,255) ) function CloseBut.DoClick() Background:Close() end local RobberyBut = vgui.Create( "DButton", Background ) RobberyBut:SetSize( 150, 30 ) RobberyBut:SetPos( 250-100, 250-100) RobberyBut:SetText("Start A Robbery") RobberyBut:SetColor( Color(255,0,0,255) ) function RobberyBut.DoClick() if not ply:Team() == TEAM_CITIZEN then DarkRP.notify( 1, 10, "You Can't Rob the bank whilst you are employed by the government!") Background:Close() end if ply:Team() == TEAM_CITIZEN then end end end)[/CODE] INIT.lua: [CODE]AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include( "shared.lua" ) function ENT:Initialize() self:SetModel( "models/props/cs_assault/MoneyPallet.mdl" ) self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end end util.AddNetworkString( "OpenBankMenu" ) function ENT:AcceptInput( name, activator, caller ) if name == "Use" and caller:IsPlayer() then net.Start( "OpenBankMenu" ) net.Send( caller ) end end[/CODE] So would I just add this to the server and client ? Init.lua :[CODE]util.AddNetworkString( "BankRobberyStart" )[/CODE] Cl_init.lua : (I have no idea what to add here so when the robbery button is pressed the networkstring is broadcast) Thank for your help. -Shadow
Net messages can be sent from the client to the server, very similarly. The client calls net.Start and later uses net.SendToServer and the server can receive it via a serverside net.Receive [url]https://wiki.garrysmod.com/page/Net_Library_Usage[/url] the function net.Receive has two parameters passed to the function you create inside it, length and ply (you should only worry about ply, which is the player sending a message to the server or else nil if it is a message from the server to a player) basically you can have a client inform the server what they just did and then the server can tell all the clients about it [editline]1st April 2016[/editline] be aware that after you figure out how to make it work, you should add serverside checks that ensure the player claiming they're robbing the bank actually makes sense; a player could maliciously spam the code that tells the server they're robbing the bank or whatever (never trust the claims sent by a client)
[QUOTE=bitches;50047233]Net messages can be sent from the client to the server, very similarly. The client calls net.Start and later uses net.SendToServer and the server can receive it via a serverside net.Receive [url]https://wiki.garrysmod.com/page/Net_Library_Usage[/url] the function net.Receive has two parameters passed to the function you create inside it, length and ply (you should only worry about ply, which is the player sending a message to the server or else nil if it is a message from the server to a player) basically you can have a client inform the server what they just did and then the server can tell all the clients about it [editline]1st April 2016[/editline] be aware that after you figure out how to make it work, you should add serverside checks that ensure the player claiming they're robbing the bank actually makes sense; a player could maliciously spam the code that tells the server they're robbing the bank or whatever (never trust the claims sent by a client)[/QUOTE] Thank you, I understand everything you have said apart from the last sentence: [QUOTE]be aware that after you figure out how to make it work, you should add serverside checks that ensure the player claiming they're robbing the bank actually makes sense; a player could maliciously spam the code that tells the server they're robbing the bank or whatever (never trust the claims sent by a client)[/QUOTE] How would I do this ? Just with a use type ? [editline]1st April 2016[/editline] I propose to do what you said by adding this to the files: init.lua: [CODE]util.AddNetworkString("BankRobberyStarted") net.Receive("BankRobberyStarted") net.Broadcast("BankRobberyStarted")[/CODE] cl_init.lua: [CODE]if ply:Team() == TEAM_CITIZEN then net.Start("BankRobberyStarted") net.SendToServer() net.Receive("BankRobberyStarted") if ply:Team() == TEAM_POLICE then chat.AddText("211S(Robbery, Silent Alarm) at the 10-20 Bank! All units please respond!") end end[/CODE] Would this code be correct ?
[QUOTE=ShadowHedge;50047295]Thank you, I understand everything you have said apart from the last sentence: How would I do this ? Just with a use type ? [editline]1st April 2016[/editline] I propose to do what you said by adding this to the files: init.lua: [CODE]util.AddNetworkString("BankRobberyStarted") net.Receive("BankRobberyStarted") net.Broadcast("BankRobberyStarted")[/CODE] cl_init.lua: [CODE]if ply:Team() == TEAM_CITIZEN then net.Start("BankRobberyStarted") net.SendToServer() net.Receive("BankRobberyStarted") if ply:Team() == TEAM_POLICE then chat.AddText("211S(Robbery, Silent Alarm) at the 10-20 Bank! All units please respond!") end end[/CODE] Would this code be correct ?[/QUOTE] The net.receive function doesn't work as all the other net stuff, so to receive data you have to put it all inside a function as a second argument of net.receive. Also what he meant with that last paragraph is that you should add some ifs checking if the player is truly robbing a bank and is not trying to trick/"hack" the system. [editline]1st April 2016[/editline] Also no, it wouldn't be correct, it would give you errors.
[QUOTE=geferon;50047883]The net.receive function doesn't work as all the other net stuff, so to receive data you have to put it all inside a function as a second argument of net.receive. Also what he meant with that last paragraph is that you should add some ifs checking if the player is truly robbing a bank and is not trying to trick/"hack" the system. [editline]1st April 2016[/editline] Also no, it wouldn't be correct, it would give you errors.[/QUOTE] Could you maybe send an example of code to show me what to do please *bump* [editline]1st April 2016[/editline] [CODE]if ply:Team() == TEAM_CITIZEN then net.Start("BankRobberyStarted") net.SendToServer() net.Receive( "BankRobberyStarted", function() if ply:Team() == TEAM_POLICE then chat.AddText("211S(Robbery, Silent Alarm) at the 10-20 Bank! All units please respond!") end end) end[/CODE] Would this be the correct code ?
[QUOTE=ShadowHedge;50047961]Could you maybe send an example of code to show me what to do please *bump* [editline]1st April 2016[/editline] [CODE]if ply:Team() == TEAM_CITIZEN then net.Start("BankRobberyStarted") net.SendToServer() net.Receive( "BankRobberyStarted", function() if ply:Team() == TEAM_POLICE then chat.AddText("211S(Robbery, Silent Alarm) at the 10-20 Bank! All units please respond!") end end) end[/CODE] Would this be the correct code ?[/QUOTE] Put the net.receive outside of any function or if, beacouse it adds a callback which will be called in any case. Also declare ply as LocalPlayer(). [editline]1st April 2016[/editline] And use proper tabbing. Also do the same thing you did with net receive but in the serverside code.
[QUOTE=geferon;50048161]Put the net.receive outside of any function or if, beacouse it adds a callback which will be called in any case. Also declare ply as LocalPlayer(). [editline]1st April 2016[/editline] And use proper tabbing. Also do the same thing you did with net receive but in the serverside code.[/QUOTE] I believe I understand what you mean know once this last post has been clarified I will mark the thread as solved. [CODE]if ply:Team() == TEAM_CITIZEN then net.Start("BankRobberyStarted") net.SendToServer() end end end) net.Receive( "BankRobberyStarted", function() if ply:Team() == TEAM_POLICE then chat.AddText("211S(Robbery, Silent Alarm) at the 10-20 Bank! All units please respond!") end)[/CODE] Would this then be the correct code if not please just make it easier and send the code as the thread is starting to fill up. Thank you for all the help provided throughout the thread :D -Shadow
[QUOTE=ShadowHedge;50048379]I believe I understand what you mean know once this last post has been clarified I will mark the thread as solved. [CODE]if ply:Team() == TEAM_CITIZEN then net.Start("BankRobberyStarted") net.SendToServer() end end end) net.Receive( "BankRobberyStarted", function() if ply:Team() == TEAM_POLICE then chat.AddText("211S(Robbery, Silent Alarm) at the 10-20 Bank! All units please respond!") end)[/CODE] Would this then be the correct code if not please just make it easier and send the code as the thread is starting to fill up. Thank you for all the help provided throughout the thread :D -Shadow[/QUOTE] The point of this thread as any others is to teach how to do it, not to give it already done. But if you wish too, here you have: [CODE] if ply:Team() == TEAM_CITIZEN then net.Start("BankRobberyStarted") net.SendToServer() end net.Receive( "BankRobberyStarted", function() if ply:Team() == TEAM_POLICE then chat.AddText("211S(Robbery, Silent Alarm) at the 10-20 Bank! All units please respond!") end end) [/CODE]
I have just done some working by myself. Sorry for asking loads of questions. But this is the code I have come to: cl_init.lua: [CODE]include( "shared.lua" ) function ENT:Draw() self:DrawModel() end local BankBalance = 25000 timer.Create( "BankBalanceChanging", 180, 0, function() BankBalance = BankBalance + math.random( 2500, 15500 ) end) local ply = LocalPlayer() net.Receive( "OpenBankMenu", function() local Background = vgui.Create( "DFrame" ) Background:SetSize( 500, 500 ) Background:Center() Background:SetTitle("") Background:SetVisible( true ) Background:ShowCloseButton( true ) Background:SetDraggable( false ) Background.Paint = function(s, w, h) draw.RoundedBox( 5, 0, 0, w, h, Color(20,255,20,255)) end Background:MakePopup() local BalanceBut = vgui.Create( "DButton", Background ) BalanceBut:SetSize( 150, 30) BalanceBut:SetPos( 250, 250 ) BalanceBut:SetText("Bank Balance") BalanceBut:SetColor(Color(0,0,0,255)) function BalanceBut.DoClick() DarkRP.notify( 1, 10, "The Bank Balance is: $"..BankBalance ) Background:Close() end local CloseBut = vgui.Create( "DButton", Background ) CloseBut:SetSize( 150, 30 ) CloseBut:SetPos( 250+100, 250+100) CloseBut:SetText("Close") CloseBut:SetColor( Color(0,191,255,255) ) function CloseBut.DoClick() Background:Close() end local RobberyBut = vgui.Create( "DButton", Background ) RobberyBut:SetSize( 150, 30 ) RobberyBut:SetPos( 250-100, 250-100) RobberyBut:SetText("Start A Robbery") RobberyBut:SetColor( Color(255,0,0,255) ) function RobberyBut.DoClick() if not ply:Team() == TEAM_CITIZEN then DarkRP.notify( 1, 10, "You Can't Rob the bank whilst you are employed by the government!") Background:Close() end if ply:Team() == TEAM_CITIZEN then net.Start("BankRobberyStarted") net.SendToServer() end end end) net.Recieve( "BankRobberyStarted", function() if ply:Team() == TEAM_POLICE then chat.AddText( Color(0,191,255), "211S(Robbery, Silent Alarm) at the 10-20 Bank! All units please respond!", Color(0,0,255), "SWAT", Color(0,191,255), "have been dispatched!") end end) [/CODE] init.lua: [CODE]AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include( "shared.lua" ) function ENT:Initialize() self:SetModel( "models/props/cs_assault/MoneyPallet.mdl" ) self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end end util.AddNetworkString( "OpenBankMenu" ) function ENT:AcceptInput( name, activator, caller ) if name == "Use" and caller:IsPlayer() then net.Start( "OpenBankMenu" ) net.Send( caller ) end end util.AddNetworkString("BankRobberyStarted") net.Receive("BankRobberyStarted", function() net.Broaadcast("BankRobberyStarted") end)[/CODE] [editline]1st April 2016[/editline] *bump*
Sorry, you need to Log In to post a reply to this thread.