Hello everyone! This is my first post on this forum and I am currently working on a logic gates simulation/circuitry system, and i cannot for the life of me figure out how to code it.
I attempted to create a system where if you click on a part named “Output”, and then click on a part named “Input1” or “Input2”, a black wire would appear between those 2 parts, and the color of the output determines the color of the input.
Here is a script for an AND gate (the game is down at the end if you want to try it):
local andGate = script.Parent
local input1 = andGate.Input1
local input2 = andGate.Input2
local output = andGate.AndOutput
local clickDetector1 = input1:FindFirstChildOfClass("ClickDetector") or error("ClickDetector not found on Input1")
local clickDetector2 = input2:FindFirstChildOfClass("ClickDetector") or error("ClickDetector not found on Input2")
--state of inputs
local isInput1On = false
local isInput2On = false
-- function to set the color of input1 based on its state
local function setInput1Color()
isInput1On = not isInput1On
input1.BrickColor = isInput1On and BrickColor.new("Shamrock") or BrickColor.new("Persimmon")
updateOutput()
end
-- function to set the color of input2 based on its state
local function setInput2Color()
isInput2On = not isInput2On
input2.BrickColor = isInput2On and BrickColor.new("Shamrock") or BrickColor.new("Persimmon")
updateOutput()
end
function updateOutput()
-- updates output based on AND logic
output.BrickColor = (isInput1On and isInput2On) and BrickColor.new("Shamrock") or BrickColor.new("Persimmon")
end
clickDetector1.MouseClick:Connect(setInput1Color)
clickDetector2.MouseClick:Connect(setInput2Color)
updateOutput()
The gate works just fine, its the wiring system that is annoying me. I have the actual wire part creation working, but i need to output some sort of “Signal” so that the color of the output is the same as the color of the input it is wired to. This is currently what im working with (this is just an example script for now between 2 parts in the workspace when i click on them)
local outputPart = nil
local wire = nil
local wiringEnabled = true
local function createWire(part)
if outputPart and (part.Name == "Input1" or part.Name == "Input2") then
wire = Instance.new("Part")
wire.Name = "Wire"
wire.Material = Enum.Material.Slate
wire.Color = Color3.new(0, 0, 0)
wire.Anchored = true
wire.CanCollide = false
wire.Size = Vector3.new(0.2, 0.2, (part.Position - outputPart.Position).Magnitude)
wire.CFrame = CFrame.new(outputPart.Position, part.Position) * CFrame.new(0, 0, -wire.Size.Z / 2)
wire.Parent = workspace
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = wire
if not part:FindFirstChildOfClass("ClickDetector") then
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
end
if not workspace[part.Name]:FindFirstChildOfClass("ClickDetector") then
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = workspace[part.Name]
end
print("Wire created between " .. outputPart.Name .. " and " .. part.Name)
wiringEnabled = false
elseif not outputPart then
warn("Please click on a part named Output first.")
else
warn("The part you clicked on is not named Input1 or Input2.")
end
end
script.Parent.MouseButton1Click:Connect(function()
if wiringEnabled then
local function onPartClicked(part)
if part.Name == "Output" then
if wire then
wire:Destroy()
wire = nil
end
outputPart = nil
wiringEnabled = true
elseif part.Name == "Wire" then
part:Destroy()
elseif outputPart and (part.Name == "Input1" or part.Name == "Input2") then
createWire(part)
end
end
for _, part in pairs(workspace:GetChildren()) do
if part:IsA("BasePart") then
local clickDetector = part:FindFirstChildOfClass("ClickDetector")
if not clickDetector then
clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
end
clickDetector.MouseClick:Connect(function()
onPartClicked(part)
end)
end
end
end
end)
(Extra info: The Input parts and outputs are inside of their respective model, so Input1 and Input2 and AndOutput (all outputs have their own output name) are in a model called AND, and this model is stored inside of object folder with all the other gates)
i have looked all over youtube and the dev forum and even tried to find models and even used ai to help me, im a pretty new dev so i need help on this stuff. Thank you!
game if you want to check it out: Logic Gates System! [Demo] - Roblox
(the wiring tool doesnt work right now, and if you use the delete tool, BE CAREFUL, it can delete the baseplate.)