I am currently making a circuit game where you can connect wires and make different components, such as working light bulbs, buzzers etc.
in the script below, is the main function of how to sockets and plugs work, when you plug a plug into a socked that is powered, it will power the plug, however in my game i realised i would need a female to female (socket to socket) meaning i need to transfer the plugs power to the socket, so the reverse of what my script originally did.
Im not sure how to do this, i have tried to make a start by using the internal boolean as a way to see if the socket can get power from the plug instead of giving out the power to the plug.
Heres my code:
local socketsFolder = script.Parent
local sockets = collectionService:GetTagged("Socket")
local cooldown = 1
local function createWeld(part1, part2)
local newweld = Instance.new("WeldConstraint")
newweld.Parent = part1
newweld.Part0 = part1
newweld.Part1 = part2
end
local function destroyWelds(part)
if part:FindFirstChildOfClass("WeldConstraint") then
part.WeldConstraint:Destroy()
end
end
for i, socket in pairs(sockets) do -- goes throught all of the sockets that have the "Socket" tag.
print(socket)
local isAlwaysPowered = socket:FindFirstChild("IsAlwaysPowered") -- wall sockets are always powered
local powered = socket:FindFirstChild("Powered") -- the variable to keep track if the SOCKET is powered as this can vary
local hitBox = socket:FindFirstChild("HitBox") -- the part the plug touches for it to know when to be plugged in.
local proximityPrompt = hitBox:FindFirstChildOfClass("ProximityPrompt")
local internal = socket:FindFirstChild("Internal")
local debounce = false
local plug -- nil value of the plug we can store later in the script.
hitBox.Touched:Connect(function (part)
if collectionService:HasTag(part, "Plug") and not debounce then --check if part is a plug
local inputPower = part:FindFirstChild("Powered")
local connected = part:FindFirstChild("Connected")
if internal.Value == true and inputPower.Value == true then
powered.Value = true
end
if connected and connected:IsA("BoolValue") and connected.Value then
return
end
connected.Value = true
plug = part
debounce = true
proximityPrompt.Enabled = true
part.CFrame = hitBox.CFrame
createWeld(plug, socket)
local dragDetector = plug:FindFirstChild("DragDetector")
if dragDetector then
dragDetector.Enabled = false
end
if isAlwaysPowered then
if powered and powered:IsA("BoolValue") and powered.Value == true then
local plugPowered = plug:FindFirstChild("Powered")
if plugPowered and plugPowered:IsA("BoolValue") then
plugPowered.Value = true
end
end
end
end
end)
powered.Changed:Connect(function (circuitclosed)
if plug then
local plugPowered = plug:FindFirstChild("Powered")
if internal.Value == true and plugPowered.Value == true then
powered.Value = true
return
end
if circuitclosed then
if plugPowered and plugPowered:IsA("BoolValue") then
plugPowered.Value = true
end
else
if plugPowered and plugPowered:IsA("BoolValue") then
plugPowered.Value = false
end
end
end
end)
if proximityPrompt then
proximityPrompt.Triggered:Connect(function()
proximityPrompt.Enabled = false
if plug then
local connected = plug:FindFirstChild("Connected")
connected.Value = false
local plugPowered = plug:FindFirstChild("Powered")
if plugPowered and plugPowered:IsA("BoolValue") then
plugPowered.Value = false
end
destroyWelds(plug)
local dragDetector = plug:FindFirstChild("DragDetector")
if dragDetector then
dragDetector.Enabled = true
end
plug = nil
wait(1) -- Add a short delay to reset debounce
debounce = false
end
end)
end
end