Is there anything I should change for proformance or something

-- Do not Remove this
-- Operator Created by Carrotoplia
-- Opensource Free Signal maker.
-- Devforum post link goes here when made!
local module = {}
local Remote = Instance.new("RemoteEvent", script)

local FireRemote = function(...)
	if game["Run Service"]:IsClient() then
		Remote:FireServer(...)
	else
		Remote:FireAllClients(...)
	end
end

local LocalSignals = {}
local LConnections = {}
local GlobalConnections = {}

if game["Run Service"]:IsClient() then
	Remote.OnClientEvent:Connect(function(cmd, id, ...)
		if cmd == "Create" then
			LocalSignals[id] = module.NewSignal(id, true)
		end
		if cmd == "Fire" then
			LocalSignals[id]:Fire("Server", ...)
		end
	end)
else
	Remote.OnServerEvent:Connect(function(plr, cmd, id, ...)
		if cmd == "Create" then
			LocalSignals[id] = module.NewSignal(id, true)
		end
		if cmd == "Fire" then
			LocalSignals[id]:Fire(plr, ...)
		end
	end)
end
	
function module.NewSignal(id, replicated, isgame)
	local Signal = {}
	local SignalM = {}
	setmetatable(Signal,SignalM)
	SignalM.__newindex = function(index, value)
		if index == "Replicated" and replicated or index == "Game" then
			Signal[index] = value
		else
			error("You Can not change a signals table.")
		end
	end
	SignalM.__type = "Signal"
	
	----- this block of code just creates Signal.Game and Signal.Replicated
	Signal.Game = false
	Signal.Replicated = false
	if isgame ~= nil then
		Signal.Game = isgame 
	end
	if replicated ~= nil then 
		Signal.Replicated = replicated 
	end
	if replicated == true and id == nil or isgame == true and id == nil then 
		error("ID Is Required for replication/game - NewSignal")
	end
	
	----------- GAME DATA SHARING -----------
	local toshare = {}
	local message = game:GetService("MessagingService")
	while true do
		if not isgame then break end
		task.wait(5)
		local status, w = pcall(message:PublishAsync("OperatorSignal", toshare))
		if not status then
			warn(w)
			task.wait(1)
			local s, w = pcall(message:PublishAsync("OperatorSignal", toshare))
			if s then
				toshare = {}
			else
				warn(w)
			end
		else
			toshare = {}
		end
	end
	
	
	
	--------------- SIGNAL FUNCTIONS -----------
	Signal.Connections = {}

	function Signal:Connect(f)
		table.insert(Signal.Connections, f)
		local a = {}
		local b = {}
		setmetatable(a, b)
		b.__type = "Connection"
		
		a.Connected = true
		function a:Disconnect()
			if table.find(Signal.Connections, f) then
				a.Connected = false
				table.remove(Signal.Connections, table.find(Signal.Connections, f))
			end
		end
		function a:Reconnect()
			if not table.find(Signal.Connections, f) then
				a.Connected = true
				table.insert(Signal.Connections, f)
			end
		end
		return a
	end

	function Signal:Fire(...)
		for _,connection in pairs(Signal.Connections) do
			connection(...)
		end
		for _,connection in pairs(LConnections) do
			connection(...)
		end
		for _,connection in pairs(GlobalConnections) do
			connection(...)
		end
		if replicated then
			FireRemote("Fire", id)
		end
		if Signal.Game then
			table.insert(toshare, {...})
		end
	end

	function Signal:ClearConnections()
		table.clone(Signal.Connections)
	end

	function Signal:Wait(...)
		local a = ...
		self:Connect(function(...)
			if a and a == ... then
				return
			else
				if not a then
					return 
				end
			end
		end)
		task.wait(99999*999999*99999*99999999*99999999) -- using multiplication to make it shorter
	end
	
	------ If an id was provided it sets it here to the id.
	if id then
		LocalSignals[id] = Signal
	end
	
	----- Creates the signal on the other side if replicated
	if Signal.Replicated then
		if game["Run Service"]:IsClient() then
			FireRemote("Create", id)
		else
			FireRemote("Create", id)
		end	
	end
	
	return Signal
end

return module
1 Like

IsClient() class that work for ModuleScript on server?

module scripts are just scripts with a tiny bit of changes and run on wherever required
IsClient() would be useless if it didnt work on server

1 Like