Skip to content

Instantly share code, notes, and snippets.

@mvyasu
Last active July 5, 2025 15:26
Show Gist options
  • Select an option

  • Save mvyasu/c94ac3f66fd4172c78970264df179dab to your computer and use it in GitHub Desktop.

Select an option

Save mvyasu/c94ac3f66fd4172c78970264df179dab to your computer and use it in GitHub Desktop.
chains together different wireable objects with wires in an easy to understand manner
--!strict
type AudioModifier = AudioEcho | AudioFader | AudioChorus | AudioReverb | AudioFlanger | AudioEqualizer | AudioCompressor | AudioDistortion | AudioPitchShifter
type AudioProcessor = AudioAnalyzer | AudioListener
type AudioOutput = AudioEmitter | AudioDeviceOutput
type AudioInput = AudioDeviceInput | AudioPlayer
export type Wireable = AudioModifier | AudioProcessor | AudioOutput | AudioInput
local function createWire(source: Wireable, target: Wireable, sourceName: string, targetName: string, parent: Instance): Wire
local newWire = Instance.new("Wire")
newWire.SourceInstance = source
newWire.TargetInstance = target
newWire.SourceName = sourceName
newWire.TargetName = targetName
newWire.Name = `{source} ({newWire.SourceName}) -> {target} ({newWire.TargetName})`
newWire.Parent = parent
return newWire
end
type extraWireInfo = {
SourceName: string?,
TargetName: string?,
Parent: Instance?,
}
type WireInfo = {
SourceInstance: Wireable,
TargetInstance: Wireable,
SourceName: string,
TargetName: string,
Parent: Instance,
}
type WireChainSetup = {WireInfo}
export type WireChainStatus = "Connected" | "Disconnected"
export type WireChain = {
to: (Wireable, extraWireInfo?) -> WireChain,
from: (Wireable, extraWireInfo?) -> WireChain,
getStatus: () -> WireChainStatus,
connect: () -> nil,
disconnect: () -> nil,
}
local function createWireChainWith(firstWireable: Wireable, currentWireChainSetup: WireChainSetup?): WireChain
local wireChainSetup = (if currentWireChainSetup then table.clone(currentWireChainSetup) else {}) :: WireChainSetup
local isConnected = false
local createdWires = {}
return {
to = function(secondWireable: Wireable, wireInfo: extraWireInfo?): WireChain
table.insert(wireChainSetup, {
SourceInstance = firstWireable,
TargetInstance = secondWireable,
SourceName = wireInfo and wireInfo.SourceName or "Output",
TargetName = wireInfo and wireInfo.TargetName or "Input",
Parent = (wireInfo and wireInfo.Parent or secondWireable) :: Instance,
})
return createWireChainWith(secondWireable, wireChainSetup)
end,
from = function(secondWireable: Wireable, wireInfo: extraWireInfo?): WireChain
table.insert(wireChainSetup, {
SourceInstance = secondWireable,
TargetInstance = firstWireable,
SourceName = wireInfo and wireInfo.SourceName or "Output",
TargetName = wireInfo and wireInfo.TargetName or "Input",
Parent = (wireInfo and wireInfo.Parent or firstWireable) :: Instance,
})
return createWireChainWith(secondWireable, wireChainSetup)
end,
getStatus = function(): WireChainStatus
return if isConnected then "Connected" else "Disconnected"
end,
connect = function()
if isConnected then
return
end
isConnected = true
for _,wireInfo in wireChainSetup do
table.insert(createdWires,
createWire(
wireInfo.SourceInstance,
wireInfo.TargetInstance,
wireInfo.SourceName,
wireInfo.TargetName,
wireInfo.Parent
)
)
end
end,
disconnect = function()
if not isConnected then
return
end
isConnected = false
local wiresToCleanup = table.clone(createdWires)
table.clear(createdWires)
for _,wire in wiresToCleanup do
wire:Destroy()
end
end,
}
end
return createWireChainWith
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment