ShoutService -- Simple Messaging System Like Scratch Broadcast

Shout service is a simple module used for sending messages across scripts
It operates very simple and easy

Shouts contain the title then the parameters
you can connect to specific titles being sent or any shout being sent
Copy and paste this into a module script to get ShoutService

-- Created by carrotoplia
local module = {}
local hooks = {}
local onshout = {}

function module.Shout(id, ...)
	if hooks[tostring(id)] == nil then
		hooks[tostring(id)]  = {}
	end
	for _,hook in pairs(hooks[tostring(id)]) do
		hook(...)
	end
	for _,hook in pairs(onshout) do
		hook(id, ...)
	end
end
function module.Hook(id, f)
	if hooks[tostring(id)] == nil then
		hooks[tostring(id)]  = {}
	end
	table.insert(hooks[tostring(id)], f)
	local a = {}
	function a:Disconnect()
		if table.find(hooks[tostring(id)], f) then
			table.remove(hooks[tostring(id)], table.find(hooks[tostring(id)], f))
		end
	end
	return a
end
function module.OnShout(f)
	table.insert(onshout, f)
	local a = {}
	function a:Disconnect()
		if table.find(onshout, f) then
			table.remove(onshout, table.find(onshout, f))
		end
	end
	return a
end

return module

Code Example

local ShoutService = require(game.ReplicatedStorage.ShoutService)
ShoutService.OnShout(function(id, ...)
    print(id)
    print(...)
end)

SSE.ShoutService.Hook("DOWN", function(...)
    print("DOWN: ".. ...)
end)

SSE.ShoutService.Shout("HW", "Hello World")
SSE.ShoutService.Shout("DOWN", "UP")

These can send any data after the title!

2 Likes

Why should developers use this over BindableEvents? Are there any benefits?

1 Like

Bindable events you have to fully input a path to the event and connect to it (and prob is more expensive)
however this you just require it once and can just send messages and get them easily + It has OnShout that allows you to get any shout sent.

I just want to point out 2 things that I noticed to not be optimal. The first one is creating a new function for each call to the “module.Hook” function. A more optimal approach would be using “:” instead of “.” and then making the “module” table a metatable for each call to the “Hook” function. Lastly, It would make more sense to create a new thread for each functions that are “Hooked”. Other than that, good job!

1 Like

wait : functions differently then .
what are the differences?

using “:” allows you to access the “self” keyword. This can be useful within modules so you don’t have to keep initializing the same function(s) each time.
For example, this can be turned from this:

local module = {}

function module:DoStuff()
   local stuff = {
      a = 3,
      obj = workspace.Part
   }
   
   function stuff:IncrementA()
      stuff.a += 1
   end   

   function stuff:Destroy()
      stuff.obj:Destroy()
   end

   return stuff
end

return module


To this:

local module = {}
module.__index = module

function module:DoStuff()
   local stuff = {
      a = 3,
      obj = workspace.Part
   }

   return setmetatable(stuff, module)
end

function module:IncrementA()
   self.a += 1
end

function module:Destroy()
   self.obj:Destroy()
end


return module
5 Likes