• Auto Broadcasting Script
    3 replies, posted
Hello everyone! I have come here in need of help. I am asking all of the lua coders to make an auto broadcasting script for me. If possible I would like the text to be yellow like the adverts on DarkRP servers and to say BROADCAST: <message>. This means I want the name of the "person" saying it to be BROADCAST. I would like the code to include what to broadcast and to broadcast every x minutes. I also want to learn something by making this post. So when you post parts of the lua code, I want to learn what it means, so please don't hesitate to tell me what everything in the script means. Cheers, Nikorev
[lua]if SERVER then -- Server util.AddNetworkString("BroadCast") -- Creating the Net Msg hook.Add("PlayerSay","blah", function() -- do stuff here, capture what they say end) -- Network it to the clients with color -- net.Start("BroadCast") -- net.WriteTable({Color(yellow), text, blah}) -- net.Broadcast -- Wrap it in timer.Create else net.Receive("BroadCast", function() local tbl = net.ReadTable() chat.AddText(unpack(tbl)) end) end[/lua] example of how it'd work out. Read the wiki a bit and you can put it together yourself. [editline]11th May 2014[/editline] keep in mind it's just startin' it off for you, that isnt real, full code lol
Since you seem to actually care about learning, I'll give this a shot for you. You don't make the player's name appear as "BROADCAST", you use the chat add function to add your custom message to the chat. [lua]-- CONFIG local broadcast_messages = { -- Table of messages, add as many as you want, remember your commas "There's a snake in my boot!", "Big kids don't cry", "Up up and away!", } local broadcast_mins = 1 -- Minute delay between each message local broadcast_random = true -- Choose random from table or sequential? local broadcast_prefix = "[BROADCAST]: " local broadcast_prefix_color = Color( 200, 0, 0 ) local broadcast_message_color = Color( 255, 200, 0 ) if SERVER then -- This can only be done by the server util.AddNetworkString( "broadcast_net" ) -- Define a unique message name local broadcast_index = 1 -- The counter just in case you choose sequential local str -- Create a localized variable which will be used to store picked messages, so we don't have to write "local" later timer.Create( "broadcast_timer", broadcast_mins *60, 0, function() -- Create an infinite timer, delay *60 because it's in seconds -- First we must pick out our message if broadcast_random then -- If we want a random message... str = table.Random( broadcast_messages ) -- Pick a random item from table else -- Otherwise... str = broadcast_messages[ ( broadcast_index -1 ) % #broadcast_messages +1 ] -- Pick the next item in the table broadcast_index = broadcast_index +1 -- Add one onto the counter end -- Now send the message net.Start( "broadcast_net" ) -- Use the unique identifier net.WriteString( str ) -- Write our message net.Broadcast() -- Send to all the clients end ) end if CLIENT then -- This part is done by the client net.Receive( "broadcast_net", function() -- This function will handle our messages chat.AddText( broadcast_prefix_color, broadcast_prefix, broadcast_message_color, net.ReadString() ) -- Add the message to the chat -- net.ReadString() == the first string that we wrote; the message end ) end[/lua] Functions and other useful links: [url=http://lua-users.org/lists/lua-l/2007-03/msg00833.html]Modulo information with indexes[/url] [url=http://wiki.garrysmod.com/page/timer/Create]timer.Create[/url] [url=http://wiki.garrysmod.com/page/chat/AddText]chat.AddText[/url] [url=http://wiki.garrysmod.com/page/Net_Library_Usage]Net Library tutorial[/url] [url=http://facepunch.com/forumdisplay.php?f=65]This place[/url] might be useful for you in the future, and for learning GLua in general.
I was looking into net.Broadcast, but I had no idea where to start with the serverside portion of the code. Thanks you for the help! I will check this out and tell you what happens later today. [editline]11th May 2014[/editline] Worked like a charm. I will look into these pages and see if I could learn a little more. Maybe I could do something really cool with this. Thank you so much! [editline]11th May 2014[/editline] [QUOTE=zerothefallen;44782919][lua]if SERVER then -- Server util.AddNetworkString("BroadCast") -- Creating the Net Msg hook.Add("PlayerSay","blah", function() -- do stuff here, capture what they say end) -- Network it to the clients with color -- net.Start("BroadCast") -- net.WriteTable({Color(yellow), text, blah}) -- net.Broadcast -- Wrap it in timer.Create else net.Receive("BroadCast", function() local tbl = net.ReadTable() chat.AddText(unpack(tbl)) end) end[/lua] example of how it'd work out. Read the wiki a bit and you can put it together yourself. [editline]11th May 2014[/editline] keep in mind it's just startin' it off for you, that isnt real, full code lol[/QUOTE] Without referring to the other code I will try to complete this. I will keep the other one as a backup.
Sorry, you need to Log In to post a reply to this thread.