Skip to content

Instantly share code, notes, and snippets.

@SigmaThetaTech
Last active May 26, 2021 22:02
Show Gist options
  • Save SigmaThetaTech/457edded0f29d7335a13190e5e11249d to your computer and use it in GitHub Desktop.
Save SigmaThetaTech/457edded0f29d7335a13190e5e11249d to your computer and use it in GitHub Desktop.
-- event Middleware
-- AvataX / OverHash
-- September 25, 2020
-- Replicates actions to client via RemoteEvent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Network = ReplicatedStorage:FindFirstChild("Network")
local storeChangeEvent
if not Network then
local Folder = Instance.new("Folder")
Folder.Name = "Network"
Folder.Parent = ReplicatedStorage
local RE = Instance.new("RemoteEvent")
RE.Name = "storeChange"
RE.Parent = Folder
end
storeChangeEvent = ReplicatedStorage:WaitForChild("Network"):WaitForChild("storeChange")
local function eventMiddleware(nextDispatch, store)
return function(action)
if typeof(action) == "function" then
print("That was weird. The dispatched action is a "..typeof(action).." named "..tostring(action))
else
nextDispatch(action)
if action.ONLY_REPLICATE_TO then -- Action has an array of players that need this information
for _, player in ipairs(action.ONLY_REPLICATE_TO) do
print("Privately firing to "..tostring(player))
storeChangeEvent:FireClient(player,action)
end
else -- All the clients need to know
storeChangeEvent:FireAllClients(action)
end
end
end
end
return eventMiddleware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment